
- 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
Update an array of strings nested within an array of objects in MongoDB
Let us create a collection with documents −
> db.demo411.aggregate( ... [ ... {$project : { ... _id : 0, ... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ... } ... } ... ] ... ) { "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] } > db.demo412.insertOne( ... { ... "Information1" : [ ... { ... "Information2" : [ ... "John", ... "David" ... ] ... }, ... { ... "Information2" : [ ... "Mike" ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e70f38b15dc524f70227683") }
Display all documents from a collection with the help of find() method −
> db.demo412.find();
This will produce the following output −
{ "_id" : ObjectId("5e70f38b15dc524f70227683"), "Information1" : [ { "Information2" : [ "John", "David" ] }, { "Information2" : [ "Mike" ] } ] }
Following is the query to update an array of strings nested within an array of objects in MongoDB −
> db.demo412.updateMany( ... { _id: ObjectId("5e70f38b15dc524f70227683") }, ... { $pull : {'Information1.$[].Information2' : "Mike" } } ... ); { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo412.find();
This will produce the following output −
{ "_id" : ObjectId("5e70f38b15dc524f70227683"), "Information1" : [ { "Information2" : [ "John", "David" ] }, { "Information2" : [ ] } ] }
- Related Articles
- Update objects in a MongoDB documents array (nested updating)?
- Deleting specific record from an array nested within another array in MongoDB
- Delete specific record from an array nested within another array in MongoDB?
- Query a nested field within an array with MongoDB
- Querying on an array of objects for specific nested documents with MongoDB?
- Can we search an array of objects in MongoDB?
- Update elements inside an array in MongoDB?
- Extract particular element in MongoDB within a Nested Array?
- Update multiple elements in an array in MongoDB?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- How to convert nested array pairs to objects in an array in JavaScript ?
- How to sort an array of objects based on the length of a nested array in JavaScript
- Sorting an array of objects by an array JavaScript
- Pull multiple objects from an array in MongoDB?
- Find by _id on an array of objects in MongoDB database?

Advertisements