
- 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
Can I get the first item in a Cursor object in MongoDB?
Yes, you can get the first item in a cursor object using findOne() method. Following is the syntax
db.yourCollectionName.findOne();
However, the following syntax is used if you want a single document in a cursor object
db.yourCollectionName.findOne({yourCondition});
We will first create a collection. Following is the query to create a collection with documents
> db.getFirstItemDemo.insertOne({"CustomerName":"Chris","CustomerAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c989059330fd0aa0d2fe4c1") } > db.getFirstItemDemo.insertOne({"CustomerName":"Larry","CustomerAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c989063330fd0aa0d2fe4c2") } > db.getFirstItemDemo.insertOne({"CustomerName":"Robert","CustomerAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c98906d330fd0aa0d2fe4c3") } > db.getFirstItemDemo.insertOne({"CustomerName":"David","CustomerAge":39}); { "acknowledged" : true, "insertedId" : ObjectId("5c989081330fd0aa0d2fe4c4") }
Following is the query to display all documents from the collection with the help of find() method
> db.getFirstItemDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c989059330fd0aa0d2fe4c1"), "CustomerName" : "Chris", "CustomerAge" : 28 } { "_id" : ObjectId("5c989063330fd0aa0d2fe4c2"), "CustomerName" : "Larry", "CustomerAge" : 26 } { "_id" : ObjectId("5c98906d330fd0aa0d2fe4c3"), "CustomerName" : "Robert", "CustomerAge" : 29 } { "_id" : ObjectId("5c989081330fd0aa0d2fe4c4"), "CustomerName" : "David", "CustomerAge" : 39 }
Following is the query to get the first item in a cursor object
> db.getFirstItemDemo.findOne();
This will produce the following output
{ "_id" : ObjectId("5c989059330fd0aa0d2fe4c1"), "CustomerName" : "Chris", "CustomerAge" : 28 }
Above, we have the first item in a cursor object. Following is the query to get a single document in a cursor object
> db.getFirstItemDemo.findOne({"CustomerAge":39});
This will produce the following output
{ "_id" : ObjectId("5c989081330fd0aa0d2fe4c4"), "CustomerName" : "David", "CustomerAge" : 39 }
- Related Articles
- How can I delete an item from an Object in MongoDB?
- How to get values of cursor in MongoDB?
- Getting only the first item for an array property in MongoDB?
- How can I use $elemMatch on first level array in MongoDB?
- Get the first and last item in an array using JavaScript?
- Search for documents matching first item in an array with MongoDB?
- How to get a saved object in MongoDB?
- Get distinct first words in a string with MongoDB?
- Golang Program To Get The First Item From The Array
- C++ Program to get the first item from the array
- Python Program to get the first item from the array
- How do I get the index of an item in Tkinter.Listbox?
- Iterate a cursor and print a document in MongoDB?
- Sort and get only the first two fields in a “$group” operation in MongoDB
- How to create a new object and get its saved object in MongoDB?

Advertisements