
- 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
Retrieving the first document in a MongoDB collection?
To retrieve the first document in a collection, you can use findOne(). Following is the syntax
var anyVariableName=db.yourCollectionName.findOne(); //To print result at MongoDB console write the variable name yourVariableName
Let us first create a collection with documents
> db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Robert","ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2325966324ffac2a7dc6d") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Chris","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2326266324ffac2a7dc6e") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Larry","ClientAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2326a66324ffac2a7dc6f") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"David","ClientAge":39}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2327566324ffac2a7dc70") }
Following is the query to display all documents from a collection with the help of find() method
> db.retrieveFirstDocumentDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca2325966324ffac2a7dc6d"), "ClientName" : "Robert", "ClientAge" : 23 } { "_id" : ObjectId("5ca2326266324ffac2a7dc6e"), "ClientName" : "Chris", "ClientAge" : 26 } { "_id" : ObjectId("5ca2326a66324ffac2a7dc6f"), "ClientName" : "Larry", "ClientAge" : 29 } { "_id" : ObjectId("5ca2327566324ffac2a7dc70"), "ClientName" : "David", "ClientAge" : 39 }
Following is the query to retrieve first document in a collection
> var firstDocumentOnly=db.retrieveFirstDocumentDemo.findOne(); > firstDocumentOnly;
This will produce the following output
{ "_id" : ObjectId("5ca2325966324ffac2a7dc6d"), "ClientName" : "Robert", "ClientAge" : 23 }
- Related Questions & Answers
- Get the top most document from a MongoDB collection
- Retrieving specific documents from collection by _id in MongoDB
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- Updating a MongoDB document and adding new keys only in the first document?
- MongoDB query to update a specific document from a collection
- Add new field to every document in a MongoDB collection?
- Retrieving Elements from Collection in C#
- How to insert a document into a MongoDB collection using Java?
- How to return the position of a document relative to the collection in MongoDB?
- Adding new property to each document in a large MongoDB collection?
- MongoDB query to add a document in an already created collection
- Accessing array values in a MongoDB collection to fetch a specific document
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- Retrieving Elements from Collection in Java- EnumerationIterator
- Retrieving Elements from Collection in Java- ListIterator
Advertisements