如何在对象数组内创建2dsphere索引?

时间:2019-09-19 14:47:51

标签: mongodb mongoose mongoose-schema

我有此架构,我想使用字段position查询起始位置附近的城市。

import mongoose, { Schema } from "mongoose";

var codcomSchema = new Schema({
    code: String,
    id: Number,
    position: {
        type: { type: String, enum: ['Point'], default: "Point" },
        coordinates: { type: [Number], index: { type: '2dsphere' } }
    }
}, { _id: false });

var citySchema = new Schema({
    name: String,
    code: String,
    zone: String,
    country: String,
    cap: [codcomSchema],
});

export default mongoose.model('city', citySchema);

索引声明正确吗?
这是我进行的查询,但返回错误。

let cities = await cityModel.find({
    "cap.position": {
         $near: {
             $maxDistance: distance,
             $geometry: {
                 type: "Point",
                 coordinates: coordinate
              }
         }
     }
});

这是错误 enter image description here

0 个答案:

没有答案
相关问题