【问题标题】:Mongoose aggregate sort and limit on a lookupMongoose 聚合排序和查找限制
【发布时间】:2020-04-23 07:38:18
【问题描述】:

有什么方法可以按createdAt 对照片进行排序,然后将它们限制为总共 500 张?

    const account = await userModel.aggregate([
        { $match: { 'shared.username': username } },
        {
          $lookup: {
            as: 'photos',
            foreignField: 'userId',
            from: 'photos',
            localField: '_id',
          },
        },
        {
          $project: {
            _id: 1.0,
            photos: 1.0,
            shared: 1.0,
          },
        },
      ]);

数据

{ 
    "_id" : ObjectId("5e10983cb182628af48e590a"), 
    "shared" : {
        "currency" : "USD", 
        "followers" : 0, 
        "following" : 0, 
        "language" : "en", 
        "loggedIn" : true, 
        "twofactor" : false, 
        "warningMessage" : "verify", 
        "email" : "email@gmail.com", 
        "fullName" : "James", 
        "username" : "57fe31142e10", 
        "location" : "/57fe31142e10"
    }, 
    "photos" : [
        {
            "_id" : ObjectId("5e117b8f227a32597cdcbb6e"), 
            "category" : "Double Deckers", 
            "previewId" : "5e10983cb182628af48e590a/0f5d0010a4e55794419c03328184cfb45984bf43.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/54265d07a962f8544a9ebdc1d876775d9eeed471.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/fe0b2016a0be2b26259aaf5152ef22edfffa0c57.jpg", 
            "createdAt" : ISODate("2020-01-05T06:00:47.756+0000"), 
            "updatedAt" : ISODate("2020-01-05T06:00:47.756+0000"), 
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5e11ab2f0f451779f70f89b1"), 
            "category" : "Single Decker", 
            "previewId" : "5e10983cb182628af48e590a/30d496e44faae1345e4c555accfdc6446fd11945.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/9293dd5517f694341a2e582df670dc9bbaba0763.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/0a038434e857b05ef8aa46da6dab01259ba2b03e.jpg", 
            "createdAt" : ISODate("2020-01-05T09:23:59.007+0000"), 
            "updatedAt" : ISODate("2020-01-05T09:23:59.007+0000"), 
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5e11aba00f451779f70f89b2"), 
            "category" : "Midi", 
            "previewId" : "5e10983cb182628af48e590a/2267d14aa642d6cee86319909177ce4eef45cbcc.jpg", 
            "published" : true, 
            "thumbnailId" : "5e10983cb182628af48e590a/9d006f1ed361540ecf9d24c807a111770cf9e44f.jpg", 
            "userId" : ObjectId("5e10983cb182628af48e590a"), 
            "zoomId" : "5e10983cb182628af48e590a/88a87440a9b7845aa44c72411edddc98ea56f6b9.jpg", 
            "createdAt" : ISODate("2020-01-05T09:25:52.019+0000"), 
            "updatedAt" : ISODate("2020-01-05T09:25:52.019+0000"), 
            "__v" : 0
        }
    ]
}

我仍然需要 _id 和共享数组

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    您可以使用$lookup with custom pipeline 并将$sort$limit 一起应用:

    const account = await userModel.aggregate([
        { $match: { 'shared.username': username } },
        {
            $lookup: {
                as: 'photos',
                let: { local_id: '$_id' },
                pipeline: [ 
                    { $match: { $expr: { $eq: [ '$$local_id', '$userId' ] } } },
                    { $sort: { 'createdAt': 1 } },
                    { $limit: 500 } 
                ],
                from: 'photos',
          }
        },
        {
            $project: {
                _id: 1.0,
                photos: 1.0,
                shared: 1.0,
            }
        },
    ]);
    

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 2017-11-08
      • 1970-01-01
      • 2016-11-17
      • 2019-08-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多