MongoDB新手在这里。
我有一个看起来像这样的“客户”文档:
{
name: "myClient",
products: [{
name: "myProduct1",
environments: [{
name: "myEnvironment1",
changeLogs: [
{ "some": "fields21" },
{ "some": "fields22" }
]
},
{
name: "myEnvironment2",
changeLogs: [
{ "some": "fields11" },
{ "some": "fields12" }
]
}
]
},
{
name: "myProduct2",
environments: [{
name: "myEnv1",
changeLogs: [
{ "some": "fields1" },
{ "some": "fields2" }
]
},
{
name: "myEnv1",
changeLogs: [
{ "some": "fields3" },
{ "some": "fields4" }
]
}
]
}]
}
所以一个客户有很多products
,它有很多environments
,它有很多changeLogs
。我希望返回changeLogs
给定环境的列表,只有environment._id
继续。
我可以client
使用这个找到正确的文档_id
:
db.clients.find({'products.environments._id': ObjectId("5a1bf4b410842200043d56ff")})
但这会返回整个client
文档。我想要的是changeLogs
从环境中返回数组_id: ObjectId("5a1bf4b410842200043d56ff")
假设我有_id
first environment
of the first product
,我想要的输出如下:
[
{ "some": "fields21" },
{ "some": "fields22" }
]
您会建议我使用什么查询来实现此目的?
非常感谢您的帮助。到目前为止的文档只是令人困惑,但我相信我最终会到达那里!