
- 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
How to compare two fields in aggregation filter with MongoDB?
For this, use aggregate() along with $filter. Let us create a collection with documents −
> db.demo137.insertOne( ... { ... Name1:"Chris", ... Name2:"David", ... Detail1:[ ... {_id:"John", Name3:"Chris"}, ... {_id:"Chris", Name3:"Chris"}, ... ], ... Detail2:[{_id:"David", Name3:"Chris"}, ... {_id:"Carol", Name3:"Chris"}, ... {_id:"John", Name3:"Chris"}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e31b4cefdf09dd6d08539a0") }
Display all documents from a collection with the help of find() method −
> db.demo137.find();
This will produce the following output −
{ "_id" : ObjectId("5e31b4cefdf09dd6d08539a0"), "Name1" : "Chris", "Name2" : "David", "Detail1" : [ { "_id" : "John", "Name3" : "Chris" }, { "_id" : "Chris", "Name3" : "Chris" } ], "Detail2" : [ { "_id" : "David", "Name3" : "Chris" }, { "_id" : "Carol", "Name3" : "Chris" }, { "_id" : "John", "Name3" : "Chris" } ] }
Following is the query to compare two fields in aggregation filter −
> db.demo137.aggregate([ ... { ... $project: { ... _id: 0, ... Name1: 1, ... Name2: 2, ... Detail1: { ... $filter: { ... input: "$Detail1", ... as: "out", ... cond: { ... $eq: ["$$out._id", "$Name1" ] ... } ... } ... }, ... Detail2: { ... $filter: { ... input: "$Detail2", ... as: "out", ... cond: { ... $eq: ["$$out._id", "$Name2" ] ... } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "Name1" : "Chris", "Name2" : "David", "Detail1" : [ { "_id" : "Chris", "Name3" : "Chris" } ], "Detail2" : [ { "_id" : "David", "Name3" : "Chris" } ] }
- Related Articles
- MongoDB query condition to compare two fields?
- Count by multiple fields with MongoDB aggregation
- How to group nested fields in MongoDB aggregation with count value in array?
- Match between fields in MongoDB aggregation framework?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- MongoDB query to group several fields using aggregation framework?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- MongoDB aggregation to combine or merge fields and then count?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- How to use $ifNull with MongoDB aggregation?
- MongoDB aggregation to get two documents with the least marks
- How do you compare two fields in MongoDB while performing an operation on one of them?
- How to filter array in subdocument with MongoDB?
- Filter documents in MongoDB if all keys exist as fields?
- How to filter some fields in objects and fetch a specific subject name value in MongoDB?

Advertisements