How does one represent MongoDB GeoJSON fields in a Mongoose Schema?

前端 未结 5 1582
猫巷女王i
猫巷女王i 2020-12-07 11:44

MongoDB 2.4 allows the use of GeoJSON objects and a slew of neat functions and indexes that I\'d like to use.

It expects GeoJSON objects to be stored in the format l

5条回答
  •  一整个雨季
    2020-12-07 12:12

    I'm about to start moving all my location references in my MongoDB from '2d' to GeoJSON, so I'll encounter the same problem.

    • Regarding the type problem, you have to follow what I did below to get it working. Mongoose correctly recognises it as a string.
    • Nested arrays; I agree that mongoose.Schema.Types.Mixed will work, but I think you can try what I did below, let me know if it works. I'm not near a PC with mongo installed to try the schema out.

    Here's how I'd define the schema. The nested array can be tweaked to work, so let me know if it doesn't.

    var LocationObject = new Schema ({
      'type': {
        type: String,
        required: true,
        enum: ['Point', 'LineString', 'Polygon'],
        default: 'Point'
      },
      coordinates: [
        [
          { type: [ Number ]
        ]
      ]
    });
    

    If you get undesired results in the nesting of the Array, try this out instead. Basically nesting in deeper.

    coordinates: [
      { type: [
        { type: [ Number ] }
      ] }
    ]
    

提交回复
热议问题