简体   繁体   English

如何根据MongoDB Mongoose中填充的引用的排序对文档进行排序?

[英]How to sort documents depending on the sort of populated reference in MongoDB Mongoose?

I'm new in MongoDB Mongoose and I don't know if it is possible to get shops sorted on their location. 我是MongoDB Mongoose的新手,我不知道是否可以根据他们的位置对商店进行分类。 I tried this query 我试过这个查询

    shop.find({},{'shopName':1,'shopLocationId':1})
    .populate({path:'shopLocationId',match:{loc:{$nearSphere:{$geometry:{type:'Point',coordinates:[50,55]}}}},select:'loc -_id'})
.exec(function (err,shops) {
        if (err)
            throw err
        console.log(shops.toString())

        })
    }) 

but I get the wrong result only shops without sort. 但我得到了错误的结果只有商店没有排序。

var ShopSchema = new Schema({
    shopName: {type: String},
    shopLocationId: {type: Schema.Types.ObjectId, ref: 'location'}
})
var LocationSchema = new Schema({
    loc:{type: Schema.Types,`enter code here`
        coordinates :[ Number]},
    locationName:{ type: String
    }
})

您可以添加排序作为选项: .populate(..., options: { sort: { 'fieldToSort': 'descending' } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM