我找不到排序修饰符的文档.唯一的见解是在单元测试中: spec.lib.query.js#L12
writer.limit(5).sort(['test', 1]).group('name')
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用:
Post.find().sort(['updatedAt', 1]);
Run Code Online (Sandbox Code Playgroud) 我正在使用猫鼬和带有 maxDistance 的近查询来过滤靠近给定 gps 位置的元素。但是,near 查询会覆盖其他排序。我想要的是在给定点的 maxDistance 内找到所有元素,然后按其他一些属性排序。这是我目前正在做的一个例子:
架构:
mongoose.Schema({
name: {
type: String,
required: true
},
score: {
type: Number,
required: true,
default: 0
},
location: {
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number]
}
},
....
});
Run Code Online (Sandbox Code Playgroud)
询问:
model.find({
"location.coordinates": {
"$near": {
"$maxDistance": 1000,
"$geometry": {
"type": "Point",
"coordinates": [
10,
10
]
}
}
}
}).sort('-score');
Run Code Online (Sandbox Code Playgroud)
在 find 之后添加 .sort 在这里没有帮助,并且无论如何都会以接近的顺序返回项目。