- 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
Search by specific field in MongoDB
Let us first create a collection with documents −
> db.demo371.insertOne({"Name":"David","CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6982ae06a1609a00af2") } > db.demo371.insertOne({"Name":"John","CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f69e2ae06a1609a00af3") } > db.demo371.insertOne({"Name":"Bob","CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6a42ae06a1609a00af4") } > db.demo371.insertOne({"Name":"Mike","CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5") }
Display all documents from a collection with the help of find() method −
> db.demo371.find();
This will produce the following output −
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" } { "_id" : ObjectId("5e57f6a42ae06a1609a00af4"), "Name" : "Bob", "CountryName" : "AUS" } { "_id" : ObjectId("5e57f6ba2ae06a1609a00af5"), "Name" : "Mike", "CountryName" : "US" }
Following is the query to search by specific fields −
> db.demo371.find({ $or: [ { Name: "David" }, { CountryName: "UK"} ] } )
This will produce the following output −
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" }
- Related Articles
- Return a specific field in MongoDB?
- Fetch specific field values in MongoDB
- Search a sub-field on MongoDB?
- Find the document by field name with a specific value in MongoDB?
- MongoDB query for specific case insensitive search
- Find all collections in MongoDB with specific field?
- Find documents that contains specific field in MongoDB?
- Project specific array field in a MongoDB collection?
- Find MongoDB documents that contains specific field?
- How to search by specific pattern in MySQL?
- MongoDB query by sub-field?
- MongoDB Query to search for records only in a specific hour?
- MongoDB aggregation to fetch documents with specific field value?
- ORDER BY specific field value first in MySQL
- Select documents grouped by field in MongoDB?

Advertisements