我有我的收藏記錄中,我希望獲取的ID爲1,但我得到的細節,而不是2倍的1如何查找mongodb中的匹配記錄?
db.mycollection.insert({"person" : [ { "id":1, "details" : { "name" : "Aswini", "Age" : 10 }}, { "id":2, "details" : { "name" : "Mahesh", "Age" : 11}}]})
的人的詳細信息,則運行
> db.mycollection.findOne({"person.id":1},{"person.details":1,"_id":0})
結果是:
{
"person" :
[
{
"details" :
{
"name" : "Aswini",
"Age" : 10
}
},
{
"details" :
{
"name" : "Mahesh",
"Age" : 11
}
}
]
}
但我需要如下:
{
"person" : [
{
"details" : {
"name" : "Aswini",
"Age" : 10
}
}
]
}
不明白我們在哪裏犯錯。請你需要你的幫助。我正在使用MongoDB,Java
其錯誤。 op不想要第一個元素,他想要檢索匹配的子數組,而不考慮位置。如果person.id子文檔位於查詢的最後位置,則不會發生這種情況 – RameshVel
您是絕對正確的。我希望子數組與索引無關。如果你有任何解決方案是MongoDB的新手,請幫忙 – aswininayak