
- 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
How to get the child of a MongoDB collection by the key?
To get the child of a collection in MongoDB, use find(). Let us create a collection with documents −
> db.demo305.insertOne( ... { ... _id: 101, ... FirstName : 'Chris', ... details : { ... "0":"102", ... "1":"10001" ... } ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo305.insertOne( ... { ... _id: 102, ... FirstName : 'David', ... details : { ... "0":"103", ... "1":"10002" ... } ... } ...); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo305.find();
This will produce the following output −
{ "_id" : 101, "FirstName" : "Chris", "details" : { "0" : "102", "1" : "10001" } } { "_id" : 102, "FirstName" : "David", "details" : { "0" : "103", "1" : "10002" } }
Following is the query to get the child of a MongoDB collection by the key −
> db.demo305.find({_id:102},{'details.0':1});
This will produce the following output −
{ "_id" : 102, "details" : { "0" : "103" } }
- Related Questions & Answers
- Changing the primary key on a MongoDB collection?
- How to sum the value of a key across all documents in a MongoDB collection?
- Get the count of the number of documents in a MongoDB Collection?
- Get the size of a collection in MongoDB using conditions?
- Find all duplicate documents in a MongoDB collection by a key field?
- How to get array from a MongoDB collection?
- Removing an object in a child collection in MongoDB?
- Get the maximum element in MongoDB collection?
- Get names of all keys in the MongoDB collection?
- Get the top most document from a MongoDB collection
- How to re-map the fields of a MongoDB collection?
- How to get the child element of a parent using JavaScript?
- How to read a specific key-value pair from a MongoDB collection?
- How to count the number of documents in a MongoDB collection?
- How to sort the documents of a MongoDB collection using java?
Advertisements