gpt4 book ai didi

node.js - 如何根据 MongoDB Mongoose 中填充的引用的类型对文档进行排序?

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:12 24 4
gpt4 key购买 nike

我是 MongoDB Mongoose 的新手,我不知道是否可以根据商店的位置对其进行排序。我尝试了这个查询

    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())

})
})

但是我得到了错误的结果,只有商店没有排序。

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' } })

关于node.js - 如何根据 MongoDB Mongoose 中填充的引用的类型对文档进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43017592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com