【问题标题】:2dsphere is not a valid type at path `index`2dsphere 不是路径“索引”处的有效类型
【发布时间】:2020-03-14 04:55:21
【问题描述】:

我在猫鼬中有 2 个架构,如下所示:

PointSchema.js

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

const PointSchema = new mongoose.Schema({
  type: {
    type: String,
    enum: ['Point']
  },
  coordinates: {
    type: [Number]
  },
  index: {
    type: '2dsphere',
    sparse: true
  }
});

module.exports = {
  PointSchema
};

DeviceSchema.js

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

const PointSchema = require('./PointSchema').PointSchema;

const DeviceSchema = mongoose.Schema({
  name: String,
  location: {
    type: PointSchema,
    default: null
  }
}, {
  collection: 'devices',
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  }
});

module.exports = mongoose.model('Device', DeviceSchema);

PointSchema.js 中是否存在问题,因为它给出了以下错误:

TypeError:无效的架构配置:2dsphere 不是有效的 在路径index处输入。

按照此文档创建 PointSchema.js:https://mongoosejs.com/docs/geojson.html

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    我用模型中的下一个配置解决了我的问题!

    module.exports = (mongoose) => {
          const DomainSchema = new mongoose.Schema({
            domain_id: { type: Number },
            name: String,
            domain_data: {
              type: { type: String },
              coordinates: { type: Array },
            },
          });
          DomainSchema.index({ domain_data: '2dsphere' });
          return mongoose.model('Domain', DomainSchema);
        };
    

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 2013-07-03
      • 2019-04-02
      • 2018-03-05
      • 2010-09-13
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多