【发布时间】:2022-11-07 08:57:22
【问题描述】:
所以我的收藏中有很多文件。每个对象都是一个用户对象,其中包含想法和想法有回复。我想要的是当回复匿名为真时,它的用户名值应该说匿名而不是用户名值。
文档
[
{
"_id": {
"$oid": "6276eb2195b181d38eee0b43"
},
"username": "abvd",
"password": "efgh",
"thoughts": [
{
"_id": {
"$oid": "62778ff975e2c8725b9276f5"
},
"text": "last thought",
"anonymous": true,
"replies": [
{
"_id": {
"$oid": "62778fff75e2c8725b9276f5"
},
"text": "new reply",
"anonymous": true,
"username": "cdf"
},
{
"_id": {
"$oid": "62778fff75e2c8725b9276f5"
},
"text": "new reply",
"anonymous": false,
"username": "cdf"
}
]
}
]
}
]
需要输出。如果您看到用户名中的值显示为匿名,即使现有文档具有“cdf”作为值
[
{
"_id": {
"$oid": "6276eb2195b181d38eee0b43"
},
"username": "abvd",
"password": "efgh",
"thoughts": [
{
"_id": {
"$oid": "62778ff975e2c8725b9276f5"
},
"text": "last thought",
"anonymous": true,
"replies": [
{
"_id": {
"$oid": "62778fff75e2c8725b9276f5"
},
"text": "new reply",
"anonymous": true,
"username": "anonymous"
},
{
"_id": {
"$oid": "62778fff75e2c8725b9276f5"
},
"text": "new reply",
"anonymous": false,
"username": "cdf"
}
]
}
]
}
]
如果您知道如何提供帮助,请告诉我。 这是包含现有文档的 MongoDB Playground URL: https://mongoplayground.net/p/WoP-3z-DMuf
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework