- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Retrieving array values from a find query in MongoDB database
To retrieve array values from a find query, use dot notation. Let us create a collection with documents −
> db.demo38.insertOne({"ClientDetails":[{"ClientId":101,"ClientName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176abccfb11e5c34d898d9") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":102,"ClientName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ac7cfb11e5c34d898da") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":103,"ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176ad0cfb11e5c34d898db") }
Display all documents from a collection with the help of find() method −
> db.demo38.find();
This will produce the following output −
{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] } { "_id" : ObjectId("5e176ad0cfb11e5c34d898db"), "ClientDetails" : [ { "ClientId" : 103, "ClientName" : "Mike" } ] }
Following is the query to retrieve array values from a find query −
> db.demo38.find({"ClientDetails.ClientName":"David"});
This will produce the following output −
{ "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { "ClientId" : 102, "ClientName" : "David" } ] }
Advertisements