为什么我将此值设为NULL

时间:2015-03-02 07:00:11

标签: mongodb mongodb-query database

当我提供findOne函数时,我得到了下面的错误。它显示为NULL,无法找到解决方案,因为有标题的记录。

 db.links.findOne({title: "MongoDB TUtor"});
    {
        "_id" : ObjectId("54f3ea83663952fddd7aaa54"),
        "title" : "MongoDB TUtor",
        "url" : "mongo.com",
        "comment" : "document-oriented db"
    }
    > db.links.findOne({title: "true"});
    null
    > db.links.findOne({title: 1});
    null

-

链接集合中的记录

 db.links.find().forEach(printjson)

{

"_id" : ObjectId("54f3ea83663952fddd7aaa54"),

"title" : "MongoDB TUtor",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{

"_id" : ObjectId("54f3ebe7663952fddd7aaa55"),

"title" : "Mongo DB handson",

"url" : "https://university.mongo.com",

"comment" : [

"NoSQL",

"Document-based"

],

"filed_on" : ISODate("2015-03-02T04:47:02.131Z"),

"meta" : {

"browser" : [

"Google Chrome",

"MAC Safari"

],

"version" : "13.4.3.7"

}

}

{

"_id" : ObjectId("54f3f77e663952fddd7aaa5a"),

"title" : "MongoDB_database",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{

"_id" : ObjectId("54f3f7b7663952fddd7aaa5c"),

"title" : "database",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{ "_id" : ObjectId("54f3fa5f663952fddd7aaa5d"), "title" : "Rocky" }

{

"_id" : ObjectId("54f3facf663952fddd7aaa5e"),

"title" : "Arnold",

"userId" : ObjectId("54f3fa5f663952fddd7aaa5d")

}

> db.links.find().pretty()

{

"_id" : ObjectId("54f3ea83663952fddd7aaa54"),

"title" : "MongoDB TUtor",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{

"_id" : ObjectId("54f3ebe7663952fddd7aaa55"),

"title" : "Mongo DB handson",

"url" : "https://university.mongo.com",

"comment" : [

"NoSQL",

"Document-based"

],

"filed_on" : ISODate("2015-03-02T04:47:02.131Z"),

"meta" : {

"browser" : [

"Google Chrome",

"MAC Safari"

],

"version" : "13.4.3.7"

}

}

{

"_id" : ObjectId("54f3f77e663952fddd7aaa5a"),

"title" : "MongoDB_database",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{

"_id" : ObjectId("54f3f7b7663952fddd7aaa5c"),

"title" : "database",

"url" : "mongo.com",

"comment" : "document-oriented db"

}

{ "_id" : ObjectId("54f3fa5f663952fddd7aaa5d"), "title" : "Rocky" }

{

"_id" : ObjectId("54f3facf663952fddd7aaa5e"),

"title" : "Arnold",

"userId" : ObjectId("54f3fa5f663952fddd7aaa5d")

}

1 个答案:

答案 0 :(得分:1)

这不是你怎么做的。您查询它的方式是查找具有"title": true"title": 1的实际文档。当然,你的文件都不符合这个条件。

如果您想知道该字段是否“实际存在”,那么您可以使用$exists运算符来测试该状态:

db.links.findOne({ "title": { "$exists": true } })

或者在查找没有匹配字段的文档时,反之亦然。