- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements
To Continue Learning Please Login
Login with Google