我有產品集合。每個產品都包含一系列項目。如何在MongoDB集合中找到與給定標準匹配的文檔和單個子文檔
> db.products.find().pretty()
{
"_id" : ObjectId("54023e8bcef998273f36041d"),
"shop" : "shop1",
"name" : "product1",
"items" : [
{
"date" : "01.02.2100",
"purchasePrice" : 1,
"sellingPrice" : 10,
"count" : 15
},
{
"date" : "31.08.2014",
"purchasePrice" : 10,
"sellingPrice" : 1,
"count" : 5
}
]
}
所以,可以請你給我一個建議,我怎麼能MongoDB的查詢檢索與該日期是等於我通過查詢作爲參數的日期只有單個項目的所有產品。
爲 「2014年8月31日」 的結果必然是:
{
"_id" : ObjectId("54023e8bcef998273f36041d"),
"shop" : "shop1",
"name" : "product1",
"items" : [
{
"date" : "31.08.2014",
"purchasePrice" : 10,
"sellingPrice" : 1,
"count" : 5
}
]
}
從3.2開始使用'$ filter'。更多在這裏https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – Veeram 2017-12-13 19:47:49