0

如果我有以下架构:


var zipSchema = new mongoose.Schema({
  zc_code             : String,
  zc_population       : Number,
  zc_households       : Number,
  zc_housevalue       : Number,
  zc_householdincome  : Number,
  zc_statecode        : String,
  zc_state            : String,
  zc_city             : String,
  zc_cityname         : String,
  modified_at         : Date, 
  center: {
    type: {type: String},
    coordinates: []
  }
})
zipSchema.index({ center: '2dsphere' });


我试试这个:


    var zipInfo = {
      zc_code: '78746',
      zc_population: 26928,
      zc_households: 10839,
      zc_housevalue: 344000,
      zc_householdincome: 100571,
      zc_latitude: '30.295657',
      zc_long: '-97.813727',
      zc_statecode: 'TX',
      zc_state: 'Texas',
      zc_city: 'AUSTIN',
      center: {
        coordinates: [-73.7567, 42.6525],
        type: 'Point'
      }
    }

    Zip.create(zipInfo, function(err) { if (err) console.log(err) })

我每次都会收到此错误:

MongoError:预期位置对象,位置数组格式不正确

我错过了什么。我搜索了 stackoverflow 并看到了 geojson 内容的几种不同设置。我什至尝试直接从 mongoosejs 测试中复制一些东西,但仍然出现错误。我处于死胡同。任何帮助,将不胜感激

4

2 回答 2

1

它对我也不起作用,但我刚刚通过命名几何字段来修复它:“loc”,如下所示:http: //docs.mongodb.org/manual/core/2dsphere/

这是我使用的示例:

var cableSchema = new mongoose.Schema({
    reference: String,
    owner: String,
    status: String,
    setup: String,
    model: Number,
    loc: {
        type: {type:String},
        coordinates: Array
    }
});
于 2013-09-16T02:58:19.297 回答
0

我用最新的猫鼬尝试了以下方法,它确实对我有用。您使用的是哪个版本?而对于 { type: {type: String } } 的问题,我相信是因为 type 也是 mongoose 关键字类型:

var zipSchema = new mongoose.Schema({
  zc_code             : String,
  zc_population       : Number,
  zc_households       : Number,
  zc_housevalue       : Number,
  zc_householdincome  : Number,
  zc_statecode        : String,
  zc_state            : String,
  zc_city             : String,
  zc_cityname         : String,
  modified_at         : Date, 
  center: {
    type: {type:String},
    coordinates: [Number]
  }
})

zipSchema.index({ center: '2dsphere' });

var zipInfo = {
      zc_code: '78746',
      zc_population: 26928,
      zc_households: 10839,
      zc_housevalue: 344000,
      zc_householdincome: 100571,
      zc_latitude: '30.295657',
      zc_long: '-97.813727',
      zc_statecode: 'TX',
      zc_state: 'Texas',
      zc_city: 'AUSTIN',
      center: {
        type: 'Point',
        coordinates: [-73.7567, 42.6525]
      }
    }

var Zip = mongoose.model('Zip', zipSchema);

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  Zip.create(zipInfo, function(err) { 
      if (err) console.log(err);

      mongoose.disconnect();

   })
});

结果是:

> db.zips.find().pretty()
{
    "zc_code" : "78746",
    "zc_population" : 26928,
    "zc_households" : 10839,
    "zc_housevalue" : 344000,
    "zc_householdincome" : 100571,
    "zc_statecode" : "TX",
    "zc_state" : "Texas",
    "zc_city" : "AUSTIN",
    "_id" : ObjectId("522e2df92aacd22e89000001"),
    "center" : {
        "type" : "Point",
        "coordinates" : [
            -73.7567,
            42.6525
        ]
    },
    "__v" : 0
}
> db.zips.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "test.zips",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "center" : "2dsphere"
        },
        "ns" : "test.zips",
        "name" : "center_2dsphere",
        "background" : true,
        "safe" : null
    }
]
> 
于 2013-09-09T20:24:38.687 回答