

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch a specific document in MongoDB with array elements
To fetch a specific document, use dot notation in MongoDB find(). Let us create a collection with documents −
> db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Oppo"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"Samsung"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile","Name":"OnePlus"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }
Display all documents from a collection with the help of find() method −
> db.demo672.find();
This will produce the following output −
{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] } { "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Samsung" } ] } { "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }
Following is the query to fetch a specific document −
> db.demo672.find({"Brand.Name":"OnePlus"});
This will produce the following output −
{ "_id" : ObjectId("5ea3eacc04263e90dac943e7"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "OnePlus" } ] }
- Related Questions & Answers
- Accessing array values in a MongoDB collection to fetch a specific document
- Make nested queries in MongoDB 4 to fetch a specific document
- Find document with array that contains a specific value in MongoDB
- How to find specific array elements in MongoDB document with query and filter with range?
- Extract a MongoDB document with a specific string
- Update a specific MongoDB document in array with $set and positional $ operator?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Query an array in MongoDB to fetch a specific value
- MongoDB query to implement nor query to fetch documents except a specific document
- Find MongoDB document with array containing the maximum occurrence of a specific value
- MongoDB query to remove array elements from a document?
- Fetch specific field values in MongoDB
- Fetch specific multiple documents in MongoDB
- Return specific MongoDB embedded document
- MongoDB query to remove a specific document
Advertisements