问题描述
我有一个 Mongo 数据库,在用户集合中我只有 1 个文档.我使用用户名过滤器执行 find()
和 findOne()
操作.我从 find()
操作中得到了我认为不正确的结果.
MongoDB shell 版本:3.2.10连接到:测试服务器有启动警告:2016-10-20T20:37:32.681-0700 我控制 [initandlisten]2016-10-20T20:37:32.681-0700 我控制 [initandlisten] ** 警告:/sys/kernel/mm/transparent_hugepage/enabled 是总是".2016-10-20T20:37:32.681-0700 I CONTROL [initandlisten] ** 我们建议将其设置为从不"2016-10-20T20:37:32.681-0700 我控制 [initandlisten]2016-10-20T20:37:32.681-0700 我控制 [initandlisten] ** 警告:/sys/kernel/mm/transparent_hugepage/defrag 是总是".2016-10-20T20:37:32.681-0700 I CONTROL [initandlisten] ** 我们建议将其设置为从不"2016-10-20T20:37:32.681-0700 我控制 [initandlisten]>使用实验室2切换到 db lab2>db.users.find(){_id":ObjectId(5807ac0765f24dd0660e4332"),用户名":avtrulzz",fname":Abc",lname":Def",电子邮件":[email protected]",密码":rootuser",手机":NumberLong(1234567890) }>db.users.findOne(){"_id" : ObjectId("5807ac0765f24dd0660e4332"),用户名":avtulzz","fname": "ABC","lname" : "定义","email" : "[email protected]",密码":rootuser",手机":NumberLong(1234567890)}>如果(db.users.find({用户名:noSuchUsername"})){...打印(用户名存在");... } 别的 {...打印(用户不存在");}用户名存在>if (db.users.findOne({username : "noSuchUsername"})) { print ("用户名存在");} else { 打印(用户不存在");}用户不存在>if (db.users.findOne({username : "avtulzz"})) { print ("用户名存在");} else { 打印(用户不存在");}用户名存在
看到 find()
操作返回用户存在,这是不正确的.findOne()
运行正常.
首先,findOne()
和find()
的基本区别:
findOne()
- 如果查询匹配,则返回第一个文档,否则返回 null.find()
- 无论匹配多少个文档,都会返回一个游标,永远不会为空.
因此,当放入 if 条件时,findOne()
可以在不匹配任何文档时转换为 false.由于 find()
返回一个游标对象并且从不返回 null,因此在放入 if 条件时将转换为 true.
find
和 findOne()
为空集合返回以下内容:
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.
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 :
这篇关于MongoDB 中的 find() 和 findOne() 方法显示不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!