[英]how to mongodb search nested documents and find exact match field
records of collections collections 的记录
i have used the below code to find but it showing all the accounts data我已使用以下代码查找,但它显示了所有帐户数据
db.getCollection('leads').find({"accounts.status":"suspended"})
after search query the result搜索后查询结果
i want to search where accounts.status=suspended but the result should show match nested documents under accounts我想搜索accounts.status=suspended但结果应该在accounts下显示匹配的嵌套文档
use $set
with $filter
after $match
在
$match
之后使用$set
和$filter
db.collection.aggregate([
{
"$match": {
"accounts.status": "suspended"
}
},
{
"$set": {
"accounts": {
"$filter": {
"input": "$accounts",
"as": "a",
"cond": {
$eq: [
"$$a.status",
"suspended"
]
}
}
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.