- 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
MongoDB query for exact match on multiple document fields
For exact match, set the values to be matched inside MongoDB $in(). Let us first create a collection with documents −
> db.demo422.insertOne({"Name":"Chris","Marks":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4059822da45b30346e1") } > db.demo422.insertOne({"Name":"Chris","Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a40a9822da45b30346e2") } > db.demo422.insertOne({"Name":"David","Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4149822da45b30346e3") } > db.demo422.insertOne({"Name":"Sam","Marks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a41e9822da45b30346e4") } > db.demo422.insertOne({"Name":"David","Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4239822da45b30346e5") }
Display all documents from a collection with the help of find() method −
> db.demo422.find();
This will produce the following output −
{ "_id" : ObjectId("5e73a4059822da45b30346e1"), "Name" : "Chris", "Marks" : 34 } { "_id" : ObjectId("5e73a40a9822da45b30346e2"), "Name" : "Chris", "Marks" : 56 } { "_id" : ObjectId("5e73a4149822da45b30346e3"), "Name" : "David", "Marks" : 78 } { "_id" : ObjectId("5e73a41e9822da45b30346e4"), "Name" : "Sam", "Marks" : 45 } { "_id" : ObjectId("5e73a4239822da45b30346e5"), "Name" : "David", "Marks" : 89 }
Following is the query to get records with exact match on multiple document fields −
> db.demo422.find({'$and': [{'Name': {'$in': ['Chris', 'David']}, 'Marks': {'$in': [34,89]}}]});
This will produce the following output −
{ "_id" : ObjectId("5e73a4059822da45b30346e1"), "Name" : "Chris", "Marks" : 34 } { "_id" : ObjectId("5e73a4239822da45b30346e5"), "Name" : "David", "Marks" : 89 }
- Related Articles
- MongoDB query for exact match
- MongoDB query for fields in embedded document?
- MongoDB query with fields in the same document?
- Search multiple fields for multiple values in MongoDB?
- MongoDB query to check the existence of multiple fields
- Performing distinct on multiple fields in MongoDB?
- MongoDB find() query for nested document?
- MongoDB query condition on comparing 2 fields?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- How to query a document in MongoDB comparing fields from an array?
- MongoDB $elemMatch to match document
- Query for multiple parameters in MongoDB?
- Display the undefined and exact MongoDB document records
- Filter query on array of embedded document with MongoDB?
- Match between fields in MongoDB aggregation framework?

Advertisements