Mongoose: Вставка геопространственного запроса

Созданный на 14 дек. 2017  ·  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 (а не отчет об ошибке), отправьте его либо в StackOverflow , либо на Gitter.

Вы с большей вероятностью получите ответ гораздо раньше на любой из них, не добавляя в список ошибок / функций, которые специалисты по сопровождению должны отсеять здесь.

Вот как я это делаю, надеюсь, это поможет

    // 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 рейтинги