问题:猫鼬无法检测到“ 2dsphere”索引
这是我的架构的样子:
export const ProviderSchema = new Schema({
address: {
location: {
coordinates: {
required: true,
type: [Number],
},
type: {
enum: ['Point'],
required: true,
type: String,
},
},
},
}, {
timestamps: true,
});
ProviderSchema.index({ 'address.location': '2dsphere' });
export const Provider = mongoose.model('Provider', ProviderSchema);
这是我的汇总的样子:
return Provider
.aggregate([
{
$geoNear: {
distanceField: 'address.distance',
near: [parseFloat(lng), parseFloat(lat)],
query,
},
},
])
我知道我的收藏集已建立索引,因为Provider.listIndexes()
返回此结果:
[
{ v: 2, key: { _id: 1 }, name: '_id_', ns: 'patients.providers' },
{ v: 2,
key: { 'address.location': '2dsphere' },
name: 'address.location_2dsphere',
ns: 'patients.providers',
background: true,
'2dsphereIndexVersion': 3
}
]
当我直接在mongodb上运行此查询时,它按预期工作:
db.providers.aggregate([
{
$geoNear: {
near: { type: 'Point', coordinates: [-104,39]},
distanceField: 'address.distance',
spherical: true
}
}
])
有人可以帮我指出我在做什么错吗,或者这肯定是一个错误吗?
版本: “ mongodb”:“ ^ 3.1.6”, “猫鼬”:“ ^ 5.2.16”,