无法使用填充对mongo / mongoose进行排序

时间:2018-07-20 17:43:55

标签: mongodb mongoose

我似乎无法对这个mongo / mongoose查询进行排序。第一个返回准确的结果,只是没有按照相反的方向排序。

const UserSchema = new Schema({  
    username         : String,
    current          : { type: Schema.Types.ObjectId, ref: 'News' },
    friends          : [{ user: { type: Schema.Types.ObjectId, ref: 'User'}, status: String }],
    profile          : {...}

})

const NewsSchema  = new Schema({
    user       : { type: Schema.Types.ObjectId, ref: 'User' },
    owner      : { type: Schema.Types.ObjectId, ref: 'User' },
    date       : Date,
    desc       : String
});

const News = mongoose.model('News', NewsSchema);
const User = mongoose.model('User', UserSchema);

我尝试过

.populate({
    path: 'friends.user',
    model: 'User',
    select: 'profile.firstname profile.lastname profile.avatar username current -_id',
    populate: {
        path: 'current',
        model: 'News',
        select: 'date owner desc',
        populate: {
            path: 'owner',
            model: 'User',
            select: 'profile.firstname profile.lastname profile.avatar username'
        },
    },
})
.sort({'current.date': -1})

以及

.populate({
    path: 'friends.user',
    model: 'User',
    select: 'profile.firstname profile.lastname profile.avatar username current -_id',
    populate: {
        path: 'current',
            model: 'News',
            select: 'date owner desc',
            options: { sort: { date: -1 } }
        },
    })

这是一个错误

User.findOne({ _id: req.userId }, 'friends -_id')
.populate({
    path: 'friends.user',
    model: 'User',
    select: 'profile.firstname profile.lastname profile.avatar username current -_id',
    populate: {
        path: 'current',
        model: 'News',
        select: 'date owner desc',
    },
        options: { 
            sort: { date: -1 }
        }
    })

{
    "message": "MongooseError: Cannot populate with `sort` on path friends.user because it is a subproperty of a document array"
}

第一个和第二个都返回

{
    "friends": [
        {
            "_id": "5b51fd7c3f4ec33546a0664f",
            "user": {
                "profile": {
                    ...
                },
                "current": {
                    "date": "2018-07-20T15:19:00.968Z"
                }
            }
        },
        {
            "_id": "5b51fdb53f4ec33546a06655",
                "user": {
                    "profile": {
                        ...
                    },
                    "current": {
                       "date": "2018-07-20T15:19:45.102Z"
                    }
                }
            }
       ]
}

应将其反转为“日期”:“ 2018-07-20T15:19:45.102Z”是最新版本。

是否有更好的更有效的方法来执行聚合和查找?

用户集合

 {
        "_id" : ObjectId("5b51fd2a3f4ec33546a06648"),
        "profile" : {
            "firstname" : "user1",
            "lastname" : "user1"
            "avatar" : "user1.png"
        }
        "username" : "user1",
        "friends" : [ 
            {
                "_id" : ObjectId("5b51fd7c3f4ec33546a0664f"),
                "user" : ObjectId("5b51fd643f4ec33546a0664c")
            }, 
            {
                "_id" : ObjectId("5b51fdb53f4ec33546a06655"),
                "user" : ObjectId("5b51fd903f4ec33546a06652")
            }
        ],
        "__v" : 5,
        "current" : ObjectId("5b51fd2a3f4ec33546a06649")
},
{
        "_id" : ObjectId("5b51fd643f4ec33546a0664c"),
        "profile" : {
            "firstname" : "user2",
            "lastname" : "user2"
            "avatar" : "user2.png"
        }
        "friends" : [ 
            {
                "_id" : ObjectId("5b51fdcd3f4ec33546a06658"),
                "user" : ObjectId("5b51fd2a3f4ec33546a06648")
            }
        ],
        "__v" : 5,
        "current" : ObjectId("5b51fd643f4ec33546a0664d"),
        "username" : "user2"
    },
    {
        "_id" : ObjectId("5b51fd903f4ec33546a06652"),
        "profile" : {
            "firstname" : "user3",
            "lastname" : "user3"
            "avatar" : "user3.png"
        },
        "friends" : [ 
            {
                "_id" : ObjectId("5b51fdce3f4ec33546a0665a"),
                "user" : ObjectId("5b51fd2a3f4ec33546a06648")
            }
        ],
        "__v" : 5,
        "current" : ObjectId("5b51fd903f4ec33546a06653"),
        "username" : "user3"
    }

新收藏

 {
        "_id" : ObjectId("5b51fd2a3f4ec33546a06649"),
        "user" : ObjectId("5b51fd2a3f4ec33546a06648"),
        "owner" : ObjectId("5b51fd2a3f4ec33546a06648"),
        "date" : ISODate("2018-07-20T15:18:02.962Z"),
        "desc" : "user1 gibberish",

        "__v" : 0
    }



    {
        "_id" : ObjectId("5b51fd643f4ec33546a0664d"),
        "user" : ObjectId("5b51fd643f4ec33546a0664c"),
        "owner" : ObjectId("5b51fd643f4ec33546a0664c"),
        "date" : ISODate("2018-07-20T15:19:00.968Z"),
        "desc" : "user2 gibberish",
        "__v" : 0
    }


    {
        "_id" : ObjectId("5b51fd903f4ec33546a06653"),
        "user" : ObjectId("5b51fd903f4ec33546a06652"),
        "owner" : ObjectId("5b51fd903f4ec33546a06652"),
        "date" : ISODate("2018-07-20T15:19:44.102Z"),
        "desc" : "user3 gibberish",
        "__v" : 0
    }

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总

User.aggregate([
  { "$match": { "_id": mongoose.Types.ObjectId(req.userId) } },
  { "$unwind": "$friends" },
  { "$lookup": {
    "from": User.collection.name,
    "let": { "friendId": "$friends.user" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$friendId" ] } } },
      { "$lookup": {
        "from": News.collection.name,
        "let": { "current": "$current" },
        "pipeline": [
           { "$match": { "$expr": { "$eq": [ "$_id", "$$current" ] } } },
         ],
         "as": "current"
      }},
      { "$unwind": "$current" }
    ],
    "as": "friends.user"
  }},
  { "$unwind": "$friends.user" },
  { "$sort": { "friends.user.current.date": 1 }},
  { "$group": {
    "_id": "$_id",
    "username": { "$first": "$username" },
    "profile": { "$first": "$profile" },
    "friends": { "$push": "$friends" }
  }}
])