
- 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
Perform $lookup to array of object id's in MongoDB?
For this, use $lookup. This performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing.
Let us first create a collection with documents −
> db.demo395.insertOne({Name:"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5e782317aa3ef9ab8ab207") } > db.demo395.insertOne({Name:"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5e782317aa3ef9ab8ab208") }
Display all documents from a collection with the help of find() method −
> db.demo395.find();
This will produce the following output −
{ "_id" : ObjectId("5e5e782317aa3ef9ab8ab207"), "Name" : "Chris" } { "_id" : ObjectId("5e5e782317aa3ef9ab8ab208"), "Name" : "David" }
Let us create a second collection with documents −
> db.demo396.insertOne({"details" : [ ... ObjectId("5e5e782317aa3ef9ab8ab207"), ... ObjectId("5e5e782317aa3ef9ab8ab208") ... ] ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e5e787817aa3ef9ab8ab209") }
Display all documents from a collection with the help of find() method −
> db.demo396.find();
This will produce the following output −
{ "_id" : ObjectId("5e5e787817aa3ef9ab8ab209"), "details" : [ ObjectId("5e5e782317aa3ef9ab8ab207"), ObjectId("5e5e782317aa3ef9ab8ab208") ] }
Following is the query to perform $lookup to array of object id's −
> db.demo396.aggregate([ ... { "$lookup": { ... "from": "demo395", ... "let": { "details": "$details" }, ... "pipeline": [ ... { "$match": { "$expr": { "$in": [ "$_id", "$$details" ] } } } ... ], ... "as": "output" ... }} ... ])
This will produce the following output −
{ "_id" : ObjectId("5e5e787817aa3ef9ab8ab209"), "details" : [ ObjectId("5e5e782317aa3ef9ab8ab207"), ObjectId("5e5e782317aa3ef9ab8ab208") ], "output" : [ { "_id" : ObjectId("5e5e782317aa3ef9ab8ab207"), "Name" : "Chris" }, { "_id" : ObjectId("5e5e782317aa3ef9ab8ab208"), "Name" : "David" } ] }
- Related Articles
- Querying object's field array values in MongoDB?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Perform MongoDB array concatenation to concatenate records
- Retrieve user id from array of object - JavaScript
- How to search in array of object in MongoDB?
- How to remove object from array in MongoDB?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Remove object from array in MongoDB?
- Set object property in an array true/false, whether the id matches with any id from another array of objects in JavaScript?
- How to prevent MongoDB from returning the object ID while finding a document?
- Increment value of an array element with array object in MongoDB
- How to compare attributes of different objects in MongoDB object array?
- MongoDB query to access an object in an array
- MongoDB query to update array object in index N?
- How to find by id in MongoDB?

Advertisements