3
假設我有這個文件:如何獲取集合中嵌套項目的值?
{
"_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
"location" : {
"geometry" : [
[ 123, 23.45321 ],
[ 124.55632, 43.256 ]
]
},
"advertisers" : {
"created_at" : ISODate("2011-07-26T21:02:19Z"),
"category" : "Infinity Pro Spin Air Brush",
"updated_at" : ISODate("2011-07-26T21:02:19Z"),
"lowered_name" : "conair",
"twitter_name" : "",
"facebook_page_url" : "",
"website_url" : "",
"user_ids" : [ ],
"blog_url" : ""
}
}
我只是想進去「廣告」讓我們說lowered_name的具體價值。
查詢,我能有這樣的語法:
db.advertisers.find({"advertisers.lowered_name" : "conair"})
但ofcourse它會返回一個等於查詢文件。我怎麼才能得到具體的價值「conair」。例如,在打印聲明中使用它:使用此類代碼將導致錯誤:
for cursor in results:
print(cursor["advertisers.lowered_name"])
這怎麼辦?我試過尋找,但嗯,我可能已經錯過了某處?
你試過'db.advertisers.distinct(「advertisers.lowered_name名單「)'? – Veeram
@Veeram嗨,我認爲獨特將仍然返回一個整個文件。像findOne一樣。我需要的只是獲得advertisers.lowered_name的價值 – Reiion
使用投影:'.find({「advertisers.lowered_name」:「conair」},{「advertisers.lowered_name」:1})' – styvane