

- 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
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 Questions & Answers
- Using regex in MongoDB findOne()
- Difference between count() and find().count() in MongoDB?
- Usage of findOne() with MongoDB?
- Difference between Constructors and Methods in Java
- Difference between RDBMS and MongoDB
- Difference between MySQL and MongoDB
- 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 the list() and listFiles() methods in Java
- Difference between title() and wm_title() methods in Tkinter class
- What is the difference between non-static methods and abstract methods in Java?
- What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?
- What is the difference between functions and methods in JavaScript?
Advertisements