- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to search for documents based on the value of adding two properties in MongoDB?
You can use aggregate framework for this. Here, we will get the sum and then match it to search for documents less than a particular number. Let us first create a collection with documents −
> db.searchDocumentsDemo.insertOne({"Value1":100,"Value2":560}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe1eedc6604c74817ce9") } > db.searchDocumentsDemo.insertOne({"Value1":300,"Value2":150}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe29edc6604c74817cea") } > db.searchDocumentsDemo.insertOne({"Value1":400,"Value2":200}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe30edc6604c74817ceb") } > db.searchDocumentsDemo.insertOne({"Value1":190,"Value2":210}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe45edc6604c74817cec") }
Following is the query to display all documents from a collection with the help of find() method −
> db.searchDocumentsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd3fe1eedc6604c74817ce9"), "Value1" : 100, "Value2" : 560 } { "_id" : ObjectId("5cd3fe29edc6604c74817cea"), "Value1" : 300, "Value2" : 150 } { "_id" : ObjectId("5cd3fe30edc6604c74817ceb"), "Value1" : 400, "Value2" : 200 } { "_id" : ObjectId("5cd3fe45edc6604c74817cec"), "Value1" : 190, "Value2" : 210 }
Following is the query to search for documents based on the value of adding two properties on the document −
> db.searchDocumentsDemo.aggregate([ ... { $project: {totalValue: { $add: [ "$Value1", "$Value2" ] } } }, ... { $match: {totalValue: {$lt: 500 }} } ... ]);
This will produce the following output −
{ "_id" : ObjectId("5cd3fe29edc6604c74817cea"), "totalValue" : 450 } { "_id" : ObjectId("5cd3fe45edc6604c74817cec"), "totalValue" : 400 }
- Related Articles
- Aggregate based on array value to sum values in different MongoDB documents?
- Search for multiple documents in MongoDB?
- How to filter documents based on an array in MongoDB?
- Search for documents with similar arrays in MongoDB and order by similarity value
- Querying array of Embedded Documents in MongoDB based on Range?
- How to search documents through an integer array in MongoDB?
- How can I search a collection to find a nested value in one of its documents in MongoDB?
- MongoDB Regex Search on Integer Value?
- Search for documents matching first item in an array with MongoDB?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- MongoDB aggregation to sum individual properties on an object in an array across documents
- How can I count the documents in an array based on the value of a specific field?
- Update quantity in MongoDB based on two conditions?
- Check for existing documents/embedded documents in MongoDB
- How to find two random documents in a MongoDB collection of 6?

Advertisements