
- 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 ID and fetch documents with $eq in MongoDB in case of array?
Use $eq operator along with find() to match ID and fetch documents. The $eq specifies equality condition. It matches documents where the value of a field equals the specified value.
Let us create a collection with documents −
> db.demo426.insert({"Ids":["110","120","101"]}); WriteResult({ "nInserted" : 1 }) > db.demo426.insert({"Ids":["100","201","401"]}); WriteResult({ "nInserted" : 1 }) > db.demo426.insert({"Ids":["501","600","700"]}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo426.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e75e50fbbc41e36cc3cae72"), "Ids" : [ "110", "120", "101" ] } { "_id" : ObjectId("5e75e51abbc41e36cc3cae73"), "Ids" : [ "100", "201", "401" ] } { "_id" : ObjectId("5e75e527bbc41e36cc3cae74"), "Ids" : [ "501", "600", "700" ] }
Following is the query match ID with $eq in MongoDB −
> db.demo426.find({"Ids":{$eq:"501"}});
This will produce the following output −
{ "_id" : ObjectId("5e75e527bbc41e36cc3cae74"), "Ids" : [ "501", "600", "700" ] }
- Related Articles
- Match MongoDB documents with fields not containing values in array?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB to fetch documents with $or Operator
- Fetch specific multiple documents in MongoDB
- Fetch specific documents with array values in MongoD
- Fetch all the ids of MongoDB documents?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to match documents that contain an array field
- Find in MongoDB documents with filled nested array and reshape the documents result
- MongoDB compound conditions to fetch documents?
- Fetch records from a subdocument array wherein id begins from 234 in MongoDB
- MongoDB query to match each element in a documents array to a condition?
- Match element in array of MongoDB?

Advertisements