我正在尝试过滤掉嵌套数组中的数据。我的数据如下:
{
"_id" : ObjectId("5836afca6dbd62de8d654h9"),
"name" : "First Test Event",
"registered_users" : [
{
"user_id" : ObjectId("5836e433e17ea92ea7a499a5"),
"user_name" : "Malavika",
"user_state" : "registered"
},
{
"user_id" : ObjectId("5836e433e17ea92ea7a455b5"),
"user_name" : "Piyush",
"user_state" : "joined"
},
{
"user_id" : ObjectId("58369783e17e6e63c35f36ac"),
"user_name" : "Jingi",
"user_state" : "notified"
}
]
},
{
"_id" : ObjectId("5836afca6dbd62de8d622b3"),
"name" : "Test Event",
"registered_users" : [
{
"user_id" : ObjectId("5836e433e17ea92ea7a499a5"),
"user_name" : "Malavika",
"user_state" : "registered"
},
{
"user_id" : ObjectId("5836e433e17ea92ea7a455b5"),
"user_name" : "Piyush",
"user_state" : "left"
},
{
"user_id" : ObjectId("58369783e17e6e63c35f36ac"),
"user_name" : "Jingi",
"user_state" : "notified"
}
]
}
在上面的集合中,我只想在“注册,加入”两种状态下获得第一个事件“第一个测试事件”的“注册用户”。
我的预期结果应该是:
{
"registered_users" : [
{
"user_id" : ObjectId("5836e433e17ea92ea7a499a5"),
"user_name" : "Malavika",
"user_state" : "registered"
},
{
"user_id" : ObjectId("58369783e17e6e63c35f36ac"),
"user_name" : "Piyush",
"user_state" : "joined"
}
]
}
所以我准备了如下查询:
db.events.aggregate([
{$match: {'registered_users.user_state': 'registered', "_id": ObjectId("5836afca6dbd62de8d654h9")}},
{$project: {
registered_users: {$filter: {
input: '$registered_users',
as: 'register',
cond: {$eq: ['$$register.user_state', 'registered']}
}},
_id: 0,
name: 0
}}
])
上述查询的问题不是一个状态“注册”,我需要一个 $in 运算符来处理多个状态“注册”、“加入”、“离开”等......
谁能帮我这个?