我需要通过userrelavance集合对供稿运行查找查询
我想要实现的是:
下面是我的Mongo集合和架构
Feed数据:
[
{
created_by:12,
company_id:1,
message:"Hi there"
created_at:timestamp
},
{
created_by:12,
company_id:1,
message:"how are you"
created_at:timestamp
}
{
created_by:13,
company_id:1,
message:"Hi",
created_at:timestamp,
},
{
created_by:13,
company_id:1,
message:"test",
created_at:timestamp,
},
{
created_by:14,
company_id:1,
message:"Hi",
created_at:timestamp
}
]
userrelavance :
{
forUser:"10",
userids:[12,13]
}
我正在执行以下查询:
db.getCollection('userrelavance').aggregate(
[
{"$unwind":"$userids"},
{
$lookup:{
from:'feeds',as:'feedObjects',let:{indicator_id:'$userids'},
pipeline:[
{$match:{$expr:{$eq:['$created_by','$$indicator_id']}}},
{$limit:2}
]
}
}
])
正在获取结果,但在userids(存储在relavance集合中)数组中为每个用户提供2个feed对象,但我需要2个feed对象,按created_at排序,同时考虑到所有userids记录
预期:仅考虑所有用户ID的2个提要对象,然后对created_at进行排序
已获取:
{
"_id" : ObjectId("5ddfc086782e53f39346b439"),
"userids" : 12,
"feedObjects" : [
{
"_id" : ObjectId("5d285ae2f44e3b584bf9205c"),
"tags" : [
1400
],
"message": "Hi there",
"company_id" : 1,
"feed_type" : "post",
"status" : 1,
"last_activity_date" : "1550839447440"
},
{
"_id" : ObjectId("5d285ae2f44e3b584bf9205d"),
"tags" : [
1400
],
"message":"How are you?"
"company_id" : 1,
"feed_type" : "greeting",
"status" : 1,
"last_activity_date" : "34534534534534534534534"
},
]
},
{
"_id" : ObjectId("5ddfc086782e53f39346b439"),
"userids" : 13,
"feedObjects" : [
{
"_id" : ObjectId("5d285ae2f44e3b584bf9205e"),
"tags" : [
1400
],
"company_id" : 1,
"feed_type" : "post",
"status" : 1,
"last_activity_date" : "34345345345345"
},
{
"_id" : ObjectId("5d285ae2f44e3b584bf9205f"),
"tags" : [
1400
],
"company_id" : 1,
"feed_type" : "post",
"status" : 1,
"last_activity_date" : "345435345345345"
},
]
}