我有三个不同的集合,具有以下架构。
> db.userSettings.find()
{
"_id" : ObjectId("595fd33167455f32373b5e07"),
"uid" : 1,
"tc" : 0,
"pv" : 1,
"is" : 1,
"ns": [],
"nkey": 1
}
> db.userAgents.find({})
{
"_id":ObjectId("595fd33167455f32373b5e07"),
"uid":1,
"tc":0,
"pv":1,
"is":1,
"ns":[],
"nid":85
}
> db.notifications.find().limit(1)
{
"_id":ObjectId("595fe20a67ec8e38a5a9d1b0"),
"t":5,
"uid":1,
"did":1,
"ir":false,
"dc":1499456010,
"h":"Vehicle Fall Detected",
"msg":"Kind attention! Vehicle number \"JSJEI288282\" experienced a fall at 6:33 am when it wasn't in motion.",
"c":2,
"idx":1
}
我想为
编写一个mongo-aggregation查询{
"uid": "for user identifier",
"dc": "for last notification in notifications collections for a user sorted by idx",
"idx": "for last notification in notifications collections for a user sorted by idx",
"dc": "for which notification.idx == userSettings.nkey",
"idx": "for which notification.idx == userSettings.nkey",
"count": "for notification.idx > userSettings.nkey"
}
我是mongo的新手,无法使用mongo聚合获取此数据。我希望(不确定)mongo在单个查询中使用聚合是可能的,如果不是比为所有收集的数据编写查询然后在服务器端过滤更好的最佳方式。