我有以下收藏记录:
> db.products.find(ObjectId("53a9a6aad901f2961403fc9b")).pretty()
{
"_id" : ObjectId("53a9a6aad901f2961403fc9b"),
"code" : "N39",
"name" : {
"en-UK" : "N39"
},
"weight" : [
90
],
"collectionId" : ObjectId("53a9a6a8d901f2961403fbe2"),
"fabric_composition" : [
{
"fabricId" : ObjectId("53a9a6a9d901f2961403fc69"),
"value" : 70
}
{
"fabricId" : ObjectId("53a9a6a9d901f2961403fc6a"),
"value" : 30
}
],
"visible" : "true",
"manufacturer" : "53a859d9d901f2e8f81ac83b"
}
和
> db.fabric.find().pretty()
{
"_id" : ObjectId("53a9a6a9d901f2961403fc69"),
"name" : [
{
"en-UK" : "Recycled Organic Cotton"
}
]
}
{
"_id" : ObjectId("53a9a6a9d901f2961403fc6a"),
"name" : [
{
"en-UK" : "Recycled Polyester"
}
]
}
如何查询mongodb集合products
以列出具有ObjectId
Recycled Organic Cotton
的fabric_composition的所有产品作为示例?
任何建议非常感谢
答案 0 :(得分:3)
您需要使用use dot notation来查询子文档:
db.products.find({
"fabric_composition.fabricId" : ObjectId("53a9a6a9d901f2961403fc69")
});
此查询将返回至少包含一个您正在查找fabricId
的子文档的所有文档。