Find () and findOne () in MongoDB showing different results

I have a Mongo database where there is only 1 document in the user collection. I'm doing a transaction find()and findOne()using a user name filter. I get the wrong operation result find().

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" : "test@yahoo.co.in", "password" : "rootuser", "mobile" : NumberLong(1234567890) }
> db.users.findOne()
{
    "_id" : ObjectId("5807ac0765f24dd0660e4332"),
    "username" : "avtrulzz",
    "fname" : "Abc",
    "lname" : "Def",
    "email" : "test@yahoo.co.in",
    "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 operation find()returns a user that exists, which is not true. findOne()behaves correctly.enter image description here

+7
source share
4 answers

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

  • findOne() - if the request matches, the first document is returned, otherwise - zero.

  • find() - , , .

, if, findOne() false, . find null, true, if.

find findOne() :

enter image description here

+7

, , - mongo boolean javascript:

findOne() , nil/null/whatever-it-is-called

find() , . .

+4

find() cursor, truthy, - .

, findOne , , null (JavaScript ), , .

> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> var cursor = db.collection.find();
> cursor;
> typeof cursor;
object
> !cursor;
false
> var document = db.collection.findOne();
> document;
null
> typeof document;
object
> !document;
true
+3

, , , ,

if (db.users.find({username : "noSuchUsername"}).toArray().length>0) {
... print ("Username exists"); 
... } else {
... print ("User does not exist"); }

   if (db.users.find({username : "noSuchUsername"}).size()>0) {
    ... print ("Username exists"); 
    ... } else {
    ... print ("User does not exist"); }

findOne , , , null, .

+1

Source: https://habr.com/ru/post/1658394/


All Articles