
- 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
Fetch specific field values in MongoDB
To fetch specific field values, use $in operator. The $in selects the documents where the value of a field equals any value in the specified array.
Let us first create a collection with documents −
> db.indexesDemo.createIndex({"StudentFirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.indexesDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de4d25ddae1f53b621dd") } > db.indexesDemo.insertOne({"StudentFirstName":"Chris","StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de5825ddae1f53b621de") } > db.indexesDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de6725ddae1f53b621df") } > db.indexesDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06de7225ddae1f53b621e0") }
Following is the query to display all documents from a collection with the help of find() method −
> db.indexesDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e06de4d25ddae1f53b621dd"), "StudentFirstName" : "John", "StudentLastName" : "Smith" } { "_id" : ObjectId("5e06de5825ddae1f53b621de"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" } { "_id" : ObjectId("5e06de6725ddae1f53b621df"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5e06de7225ddae1f53b621e0"), "StudentFirstName" : "David", "StudentLastName" : "Miller" }
Following is the query to indexes −
> db.indexesDemo.find( ... { StudentFirstName: { $in: [ "John", "David" ] } }, ... { _id: 0, StudentLastName: 0 } ... );
This will produce the following output −
{ "StudentFirstName" : "David" } { "StudentFirstName" : "John" } { "StudentFirstName" : "John" }
- Related Articles
- MongoDB aggregation to fetch documents with specific field value?
- Fetch specific multiple documents in MongoDB
- Accessing array values in a MongoDB collection to fetch a specific document
- Unable to implement $addToSet in MongoDB to fetch values of a single field?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Return a specific field in MongoDB?
- Search by specific field in MongoDB
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to add up the values of a specific field in documents
- Update all the values of a field with a specific string in MongoDB?
- Fetch specific documents with array values in MongoD
- Fetch a specific document in MongoDB with array elements
- MongoDB query to fetch array values
- Fetch values by ignoring a specific one in JavaScript?

Advertisements