Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find() and findOne() methods in MongoDB showing different results

I have a Mongo database where in the users collection I have just 1 document. I do a find() and a findOne() operations using the username filter. I get what I think is an incorrect result from find() operation.

MongoDB shell version: 3.2.10
connecting to: test
Server has startup warnings: 
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] 
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] 
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-10-20T20:37:32.681-0700 I CONTROL  [initandlisten] 
> use lab2
switched to db lab2
> db.users.find()
{ "_id" : ObjectId("5807ac0765f24dd0660e4332"), "username" : "avtrulzz", "fname" : "Abc", "lname" : "Def", "email" : "[email protected]", "password" : "rootuser", "mobile" : NumberLong(1234567890) }
> db.users.findOne()
{
    "_id" : ObjectId("5807ac0765f24dd0660e4332"),
    "username" : "avtrulzz",
    "fname" : "Abc",
    "lname" : "Def",
    "email" : "[email protected]",
    "password" : "rootuser",
    "mobile" : NumberLong(1234567890)
}
> if (db.users.find({username : "noSuchUsername"})) {
... print ("Username exists"); 
... } else {
... print ("User does not exist"); }
Username exists
> if (db.users.findOne({username : "noSuchUsername"})) { print ("Username exists");  } else { print ("User does not exist"); }
User does not exist
> if (db.users.findOne({username : "avtrulzz"})) { print ("Username exists");  } else { print ("User does not exist"); }
Username exists

See the find() operation is returning user exists which is not true. findOne() is behaving correctly.enter image description here

like image 408
Anuvrat Tiku Avatar asked Oct 21 '16 04:10

Anuvrat Tiku


People also ask

What is the difference between find and findOne in MongoDB?

The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor.

Is findOne faster than find in MongoDB?

find() returns a cursor whereas findOne() returns the exact document. It is faster to use find() + limit() because findOne() will always read + return the document if it exists. find() just returns a cursor (or not) and only reads the data if you iterate through the cursor.

Which of the following is incorrect statement about Find and findOne operations in MongoDB?

Q 7 - Which of the following is incorrect statement about find and findOne operations in MongoDB? A - find() returns all the documents in a collection while findOne() retrieves only the first one.

What is the output of a find () command in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


2 Answers

First of all, basic difference between findOne() and find():

  • findOne() - if query matches, first document is returned, otherwise null.

  • find() - no matter the number of documents matched, a cursor is returned, never null.

So when put in an if condition, findOne() can convert to false when it doesn't match any document. As find() returns a cursor object and never returns null, will convert to true when put in an if condition.

find and findOne() return the following for empty collection :

enter image description here

like image 167
inaitgaJ Avatar answered Sep 22 '22 06:09

inaitgaJ


The find() method will return an array, even if no documents match the search criteria. An empty array still exists, so it will act as truthy.

findOne() will either return exactly one document, or undefined. Undefined, by definition, is a falsy value.

When you know you will be searching for just one document, use findOne() to get a more accurate representation.

like image 35
jlewis90 Avatar answered Sep 23 '22 06:09

jlewis90