
- 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 documents with array values in MongoD
To fetch specific documents, use limit() along with toArray(). The toArray() method returns an array that contains all the documents from a cursor. Let us create a collection with documents −
> db.demo482.insertOne({_id:1,"StudentInformation":[{"Name":"Chris","Age":21}]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo482.insertOne({_id:2,"StudentInformation":[{"Name":"Bob","Age":23}]}); { "acknowledged" : true, "insertedId" : 2 } > db.demo482.insertOne({_id:3,"StudentInformation":[{"Name":"David","Age":20}]}); { "acknowledged" : true, "insertedId" : 3 }
Display all documents from a collection with the help of find() method −
> db.demo482.find();
This will produce the following output −
{ "_id" : 1, "StudentInformation" : [ { "Name" : "Chris", "Age" : 21 } ] } { "_id" : 2, "StudentInformation" : [ { "Name" : "Bob", "Age" : 23 } ] } { "_id" : 3, "StudentInformation" : [ { "Name" : "David", "Age" : 20 } ] }
Following is the query to fetch specific documents with limit() −
> db.demo482.find({}).limit(2).toArray();
This will produce the following output −
[ { "_id" : 1, "StudentInformation" : [ { "Name" : "Chris", "Age" : 21 } ] }, { "_id" : 2, "StudentInformation" : [ { "Name" : "Bob", "Age" : 23 } ] } ]
- Related Articles
- Fetch specific multiple documents in MongoDB
- MongoDB aggregation to fetch documents with specific field value?
- Fetch specific values from array of objects in JavaScript?
- MongoDB query to match documents with array values greater than a specific value
- Fetch specific field values in MongoDB
- Fetch a specific document in MongoDB with array elements
- Accessing array values in a MongoDB collection to fetch a specific document
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Match MongoDB documents with fields not containing values in array?
- MySQL query to fetch specific records matched from an array (comma separated values)
- MongoDB to fetch documents with $or Operator
- Fetch values by ignoring a specific one in JavaScript?
- Fetch specific rows from a MySQL table with duplicate column values (names)?

Advertisements