
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB concurrent update with sub collection?
For update, simply use update(). Use the $push operator to append a specified value and the dot notation to reach the sub collection and update inside update().
Let us create a collection with documents −
> db.demo547.insertOne( ... { ... Name : "Chris", ... Test : ... { ... "FirstTest" : ... { ... Scores: [56,29,76] ... }, ... "SecondTest" : ... { ... Scores: [98,91,78] ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8e2d579e5f92834d7f05dd") }
Display all documents from a collection with the help of find() method −
> db.demo547.find();
This will produce the following output −
{ "_id" : ObjectId("5e8e2d579e5f92834d7f05dd"), "Name" : "Chris", "Test" : { "FirstTest" : { "Scores" : [ 56, 29, 76 ] }, "SecondTest" : { "Scores" : [ 98, 91, 78 ] } } }
Following is the query for concurrent update with subcollection −
> db.demo547.update({"Name":"Chris"}, { $push:{ "Test.FirstTest.Scores" : 99}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo547.find();
This will produce the following output −
{ "_id" : ObjectId("5e8e2d579e5f92834d7f05dd"), "Name" : "Chris", "Test" : { "FirstTest" : { "Scores" : [ 56, 29, 76, 99 ] }, "SecondTest" : { "Scores" : [ 98, 91, 78 ] } } }
- Related Articles
- How to update MongoDB collection using $toLower?
- Thread Safe Concurrent Collection in C#
- MongoDB query for capped sub-collection in an array
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to update a specific document from a collection
- Query MongoDB collection starting with _?
- How to update an existing document in MongoDB collection using Java?
- How to Update multiple documents in a MongoDB collection using Java?
- Removing an array element from MongoDB collection using update() and $pull
- How to update a single field in a capped collection in MongoDB?
- Working with MongoDB $sort for sub array document
- How to update or modify the existing documents of a collection in MongoDB?
- Update MongoDB variable value with variable itself?
- Filter sub documents by sub document in MongoDB?
- MongoDB query to update a MongoDB row with only the objectid

Advertisements