Node.js 2dsphere不是路径`索引处的有效类型`
我在mongoose中有两个模式,如下所示: PointSchema.jsNode.js 2dsphere不是路径`索引处的有效类型`,node.js,mongoose,Node.js,Mongoose,我在mongoose中有两个模式,如下所示: PointSchema.js const mongoose = require('mongoose'); const Schema = mongoose.Schema; const PointSchema = new mongoose.Schema({ type: { type: String, enum: ['Point'] }, coordinates: { type: [Number] }, ind
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
};
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);
DeviceSchema.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
};
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
不是有效的
在路径索引处键入
按照此文档创建PointSchema.js:我解决了模型中下一个配置的问题
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);
};