
- 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
Query an array of embedded documents in MongoDB and push another?
For this, use $push along with update. Let us create a collection with documents −
> db.demo573.insertOne( ... { ... '_id' :101, ... 'SearchInformation' : [ ... { ... 'Site' : 'Facebook.com', ... 'NumberOfHits' : 100 ... }, ... { ... 'Site' : 'Twitter.com', ... 'NumberOfHits' : 300 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo573.find();
This will produce the following output −
{ "_id" : 101, "SearchInformation" : [ { "Site" : "Facebook.com", "NumberOfHits" : 100 }, { "Site" : "Twitter.com", "NumberOfHits" : 300 } ] }
Following is how to query an array of embedded documents in MongoDB −
> db.demo573.update({ ... _id: 101, ... "SearchInformation.Site": { ... $nin: ["Google.com"] ... } ... }, { ... $push: { ... "SearchInformation": { ... 'Site' : 'Google.com', ... 'NumberOfHits' : 10000 ... } ... } ... }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo573.find().pretty();
This will produce the following output −
{ "_id" : 101, "SearchInformation" : [ { "Site" : "Facebook.com", "NumberOfHits" : 100 }, { "Site" : "Twitter.com", "NumberOfHits" : 300 }, { "Site" : "Google.com", "NumberOfHits" : 10000 } ] }
- Related Articles
- MongoDB - Query embedded documents?
- Implement MongoDB $push in embedded document array?
- MongoDB query to push document into an array
- Querying array of Embedded Documents in MongoDB based on Range?
- Check for existing documents/embedded documents in MongoDB
- Updating Nested Embedded Documents in MongoDB?
- Filter query on array of embedded document with MongoDB?
- MongoDB query to match documents that contain an array field
- Appending an entry in one to many embedded documents with MongoDB
- MongoDB query to find matching documents given an array with values?
- MongoDB query to update an array element matching a condition using $push?
- How to push an array in MongoDB?
- Updating an array with $push in MongoDB
- MongoDB query for documents matching array, irrespective of elements order
- MongoDB query to convert an array to a map of documents with n attributes?

Advertisements