Mongoose: 地理空间查询插入

创建于 2017-12-14  ·  3评论  ·  资料来源: Automattic/mongoose

您要请求功能还是报告错误

可能的错误

目前的行为是什么?

无法在地理空间中保存坐标

很抱歉来到这里,但我找不到解决方案。

这是我的架构:

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var userSchema   = new Schema({
  email: {
      type: String,
      required: true,
      unique: true
    },
    password: {type: String, required: true},
    name: {type: String, required: true},
    type: {type: Boolean, default: false, required: true},
    geo: {
      type: { type: String, enum: "Point", default: "Point" },
      coordinates: { type: [ Number ],   default: [ 0,0 ] }
    }
});

userSchema.index({geo: '2dsphere'})
const User = mongoose.model('User', userSchema);
module.exports = User

我正在尝试插入一条新记录:

db.users.insert({email: '[email protected]', password: 'tebrwrw', name: 'teste', type: true, geo: { coordinates: [ 40, 5 ]} })

我收到此错误:

WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 16755,
                "errmsg" : "Can't extract geo keys: { _id: ObjectId('5a31d79f369885e770a989b7'), email: \"[email protected]\", password: \"tebrwrw\", name: \"teste\", type: true, geo: { coordinates: [ 40.0, 5.0 ] } }  unknown GeoJSON type: { coordinates: [ 40.0, 5.0 ] }"
        }
})

请提及您的 node.js、mongoose 和 MongoDB 版本。
最新的

最有用的评论

这是我的做法,希望对您有所帮助

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

所有3条评论

创建新问题时引用参考的贡献指南:

如果您有关于 Mongoose 的问题(不是错误报告),请将其发布到StackOverflowGitter

您更有可能更快地获得对其中任何一个的响应,同时不会增加维护人员需要在这里筛选的错误/功能的积压。

这是我的做法,希望对您有所帮助

    // in the schma declaration
    'loc': { type: { type: String, enum: "Point", default: "Point" }, coordinates: { type: [Number], default: [0,0] } }

    // after schema declaration*
    schema.index( { loc: "2dsphere" } );

    // and then, how I create it
    model.loc = { type: "Point", coordinates: [newarena.lng, newarena.lat] };

@Inateno谢谢...是吗!!

此页面是否有帮助?
0 / 5 - 0 等级