
- 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 query to match documents whose _id is in an array as part of a subdocument?
Let us create a collection with documents −
> db.demo568.insertOne({ _id: 101, details: [ {id : 101 }, { id:103 } ] }); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo568.find();
This will produce the following output −
{ "_id" : 101, "details" : [ { "id" : 101 }, { "id" : 103 } ] } Following is the query to create second collection: > db.demo569.insertOne({ _id: 101, details: "John" }) { "acknowledged" : true, "insertedId" : 101 } > db.demo569.insertOne({ _id: 102, details: "Chris" }) { "acknowledged" : true, "insertedId" : 102 } > db.demo569.insertOne({ _id: 103, details: "David" }) { "acknowledged" : true, "insertedId" : 103 }
Display all documents from a collection with the help of find() method −
> db.demo569.find();
This will produce the following output −
{ "_id" : 101, "details" : "John" } { "_id" : 102, "details" : "Chris" } { "_id" : 103, "details" : "David" }
Following is the query to match documents whose _id is in an array as part of a subdocument −
> db.demo569.find({ '_id': { '$in': db.demo568.distinct('details.id', {'_id': 101}) }})
This will produce the following output −
{ "_id" : 101, "details" : "John" } { "_id" : 103, "details" : "David" }
- Related Articles
- MongoDB query to match documents that contain an array field
- MongoDB query to match each element in a documents array to a condition?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- How to query documents by a condition on the subdocument in MongoDB?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to collectively match intersection of documents against a field
- MongoDB to sort by subdocument match?
- MongoDB query to find documents whose array contains a string that is a substring of a specific word
- MongoDB query for documents whose array elements does not have a specific value
- Query an array of embedded documents in MongoDB and push another?
- Fetch records from a subdocument array wherein id begins from 234 in MongoDB
- MongoDB query on nth element (variable index) of subdocument array
- MongoDB query to convert an array to a map of documents with n attributes?
- MongoDB query to match and remove element from an array?
- How to find documents with exactly the same array entries as in a MongoDB query?

Advertisements