2015-03-02 77 views
0

當我提供findOne函數時,我得到下面的錯誤。它顯示NULL,找不到解決方案,因爲有標題的記錄。爲什麼Iam將此值作爲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") 

} 
+0

沒有標題爲「真」的文檔 - 您期望什麼? – 2015-03-02 07:12:43

回答

1

這不是你怎麼做。您查詢的方式是查找實際文檔,該文檔有"title": true"title": 1。當然,你的文件都不符合這個條件。

當你想看看那場「居然有」,那麼你使用$exists操作來測試存在:

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

或天然與相反的情況下尋找不文檔時有匹配的字段。