- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to get values of cursor in MongoDB?
To get values of cursor in MongoDB, use hasNext(). Let us create a collection with documents −
> db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95303d395bdc21346c5") } > db.demo191.insertOne({"EmployeeId":2,"EmployeeName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95f03d395bdc21346c6") } > db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad96803d395bdc21346c7") } > db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad97003d395bdc21346c8") }
Display all documents from a collection with the help of find() method −
> db.demo191.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris Brown" } { "_id" : ObjectId("5e3ad95f03d395bdc21346c6"), "EmployeeId" : 2, "EmployeeName" : "David Miller" } { "_id" : ObjectId("5e3ad96803d395bdc21346c7"), "EmployeeId" : 1, "EmployeeName" : "John Doe" } { "_id" : ObjectId("5e3ad97003d395bdc21346c8"), "EmployeeId" : 1, "EmployeeName" : "John Smith" }
Following is the query to get values of cursor object −
> var cursor = db.demo191.find( { "EmployeeId":1 } ); > while (cursor.hasNext()) { ... print(tojson(cursor.next())); ...}
This will produce the following output −
{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris Brown" } { "_id" : ObjectId("5e3ad96803d395bdc21346c7"), "EmployeeId" : 1, "EmployeeName" : "John Doe" } { "_id" : ObjectId("5e3ad97003d395bdc21346c8"), "EmployeeId" : 1, "EmployeeName" : "John Smith" }
- Related Articles
- How to get unique values from MongoDB collection?
- How to get distinct list of sub-document field values in MongoDB?
- How to get only values in arrays with MongoDB aggregate?
- How to get max values for distinct elements in MongoDB
- Can I get the first item in a Cursor object in MongoDB?
- Get distinct record values in MongoDB?
- How to loop through collections with a cursor in MongoDB?
- MongoDB query to get only distinct values
- Get the duplicate values of a field in MongoDB?
- Get Distinct Values with Sorted Data in MongoDB?
- Get distinct values from a column in MongoDB?
- MongoDB query to get distinct FirstName values from documents
- Get the length of distinct values in an array with MongoDB
- Get the last two values with MongoDB
- MongoDB aggregate to get the count of field values of corresponding duplicate names?

Advertisements