Mongodb查找数据的限制,偏移量和排序

时间:2019-12-02 13:42:19

标签: mongodb aggregate limit lookup offset

我需要通过userrelavance集合对供稿运行查找查询

我想要实现的是:

  1. 我需要在供稿列表中显示用户的相关供稿
  2. 相关的供稿应按created_at进行排序,并考虑userreleavance集合中的所有用户ID。
  3. 我需要保留一个极限值和偏移值,以便可以对我的相关供稿进行分页
  4. 查找中的内容应按created_at(时间戳记)排序获取

下面是我的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"
            }, 
        ]
    }

0 个答案:

没有答案