- 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
Difference between find() and findOne() methods in MongoDB?
The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor.
Let us implement the concept of find() and findOne() and create a collection with documents −
> db.createCollection('emptyCollection'); { "ok" : 1 }
Let us count how many documents are in the above collection −
> db.emptyCollection.count();
This will produce the following output −
0
There is no document present in the above collection.
Following is the query to check the result with findOne() −
> if(db.emptyCollection.findOne()){print("Returns Cursor")} else {print("Not returning cursor")}
This will produce the following output −
Not returning cursor
Following is the query to check the result with find() −
> if(db.emptyCollection.find()){print("Returns Cursor")} else {print("Not returning cursor")}
This will produce the following output −
Returns Cursor
- Related Articles
- Using regex in MongoDB findOne()
- Usage of findOne() with MongoDB?
- Difference between count() and find().count() in MongoDB?
- Difference between Constructors and Methods in Java
- Difference between shift() and pop() methods in Javascript
- Difference between push() and unshift() methods in javascript
- Difference between test () and exec () methods in Javascript
- Difference between BeforeClass and BeforeTest methods in TestNG
- Difference between MySQL and MongoDB
- Difference between RDBMS and MongoDB
- Difference between Hadoop and MongoDB
- Can we work MongoDB findOne() with long type _id?
- Difference between the list() and listFiles() methods in Java
- Difference between title() and wm_title() methods in Tkinter class
- Explain the difference between functions and Methods in Swift

Advertisements