【发布时间】:2020-01-24 18:26:40
【问题描述】:
我有一个这种格式的 mongoDb 文档
{
account_key : "tiziano",
notifications : [
{ address: "tiziano@myemail.com" , notification_type: ['booking_confirmation','booking_deletion'] },
{ address: "tiziano@youremail.com" , notification_type: ['booking_confirmation'] },
{ address: "tiziano@yxyzEmail.com" , notification_type: ['credit_card_expiration'] },
]
}
并且该集合包含 n 个这种形式的文档。 我正在尝试获取具有一个特定 account_key 的所有文档,并且对于与搜索匹配的每个文档,我希望返回与特定条件匹配的 notification_type。例如,我想退回所有具有 notification_type booking_confirmation 的通知
{
account_key: "tiziano",
notifications : [
{ address: "tiziano@myemail.com" , notification_type: ['booking_confirmation','booking_deletion'] },
{ address: "tiziano@youremail.com" , notification_type: ['booking_confirmation'] }
]
}
这是我正在使用的查询,但它没有返回我想要的内容
db.getCollection("accounts").aggregate([
{ $match : { account_key : "acme" }},
{ $project : {
notification_contact: {
$filter: {
input: "$notifications",
as: "notification",
cond: { $eq : ["$$notification.notification_type", "booking_created"] }
}
}
}}
])
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework