
- 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
Using $addFields pipeline and run with the MongoDB $filter operator
Let us first create a collection with documents −
> db.demo118.insertOne( ... { ... "Id" : "101", ... "Name" : "Chris", ... "Subjects" : [ ... "MySQL", ... "MongoDB", ... "Java" ... ], ... "Details" : [ ... { ... "Name" : "David", ... S:"MongoDB" ... }, ... { ... "Name" : "Bob", ... S:"Python" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2f0c0cd8f64a552dae6364") }
Display all documents from a collection with the help of find() method −
> db.demo118.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f0c0cd8f64a552dae6364"), "Id" : "101", "Name" : "Chris", "Subjects" : [ "MySQL", "MongoDB", "Java" ], "Details" : [ { "Name" : "David", "S" : "MongoDB" }, { "Name" : "Bob", "S" : "Python" } ] }
Following is the query to use $addFields pipeline −
> db.demo118.aggregate([ ... { ... "$addFields": { ... "Details": { ... "$filter": { ... "input": "$Details", ... "as": "out", ... "cond": { "$in": ["$$out.S", "$Subjects"] } ... } ... } ... } ... } ... ]).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e2f0c0cd8f64a552dae6364"), "Id" : "101", "Name" : "Chris", "Subjects" : [ "MySQL", "MongoDB", "Java" ], "Details" : [ { "Name" : "David", "S" : "MongoDB" } ] }
- Related Articles
- Using aggregation pipeline to fetch records in MongoDB
- Using positional operator with hierarchies in MongoDB?
- MongoDB pull with positional operator?
- Filter documents in MongoDB using simple query?
- How to query MongoDB using the $ne operator?
- Find values with OR operator in MongoDB and format the result.?
- How to filter array in subdocument with MongoDB?
- How to run MongoDB shell using mongos command?\n
- Select multiple values with MongoDB OR operator
- MongoDB to fetch documents with $or Operator
- Filter query on array of embedded document with MongoDB?
- How to find specific array elements in MongoDB document with query and filter with range?
- How to query with nand operator in MongoDB?
- Filter array with filter() and includes() in JavaScript
- How to compare two fields in aggregation filter with MongoDB?

Advertisements