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

前端 未结 5 1573
猫巷女王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:17

    Mongoose now officially supports this.

    In a nutshell, what you do is, for that schema, you use the typeKey setting to tell mongoose to use a different key for type information. Here is an example:

    var schema = new Schema({
      // Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates'
      loc: { type: String, coordinates: [Number] },
      // Mongoose interprets this as 'name is a String'
      name: { $type: String }
    }, { typeKey: '$type' }); // A '$type' key means this object is a type declaration
    

    So now instead of declaring type info with the type property, you use $type. This works at the schema level so use it in the schemas that need it.

提交回复
热议问题