
- 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
Match between fields in MongoDB aggregation framework?
You can use $cmp operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.matchBetweenFieldsDemo.insertOne({"FirstValue":40,"SecondValue":70}); { "acknowledged" : true, "insertedId" : ObjectId("5c92c9625259fcd19549980d") } > db.matchBetweenFieldsDemo.insertOne({"FirstValue":20,"SecondValue":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c92c96b5259fcd19549980e") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.matchBetweenFieldsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c92c9625259fcd19549980d"), "FirstValue" : 40, "SecondValue" : 70 } { "_id" : ObjectId("5c92c96b5259fcd19549980e"), "FirstValue" : 20, "SecondValue" : 5 }
Here is the query to $match between fields in aggregation framework −
> db.matchBetweenFieldsDemo.aggregate([ ... {$project: { ... ... firstGreaterThanOrNot: {$cmp: ['$FirstValue', '$SecondValue']} ... }}, ... {$match: {firstGreaterThanOrNot:{$gt: 0}}} ... ]);
The following is the output −
{ "_id" : ObjectId("5c92c96b5259fcd19549980e"), "firstGreaterThanOrNot" : 1 }
- Related Articles
- MongoDB aggregation framework match OR is possible?
- MongoDB query to group several fields using aggregation framework?
- MongoDB query (aggregation framework) to match a specific field value
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?
- Count by multiple fields with MongoDB aggregation
- MongoDB aggregation framework to sort by length of array?
- Does aggregation query with $match works in MongoDB?
- How to compare two fields in aggregation filter with MongoDB?
- Working with Aggregation to match all the values in MongoDB
- Get the average of an entire field using the aggregation framework in MongoDB?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- MongoDB aggregation to combine or merge fields and then count?

Advertisements