如何用猫鼬添加 GeoJson 数据?
How to add GeoJson data with mongoose?
我正在发送对象来创建用户模型。
"{
type: 'Point',
coordinates: [ 25.2239771, 51.4993224 ]
}"
而且,这是我创建的猫鼬模式。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserProfileSchema = new Schema(
{
userId: {
type: String,
// required: true,
unique: true,
},
userFirstName: {
type: String,
// required: true,
},
userLastName: {
type: String,
// required: true,
},
userGender: {
type: String,
// required: true,
},
userCoordinates: {
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
index: '2dsphere',
},
},
},
{ collection: 'userprofilemodels' }
);
module.exports = UserProfile = mongoose.model(
'userprofilemodels',
UserProfileSchema
);
这是我用来添加 geoJson 类型文件的代码。但是,我收到一个错误。
我还尝试在定义架构后添加索引
await new userProfileModel({
userId,
userFirstName,
userLastName,
userCoordinates,
})
.save()
.then(() => {
console.log('it worked!');
res.send('worked!');
})
.catch((error) => {
console.log('did not worl')
// console.log(error);
});
如果我排除 userCoordinates,则它有效。所以,def 我的 geoJson 对象是错误的。但是,我不知道哪里出错了。
取自猫鼬 documentation 看来 GeoJSON 类型不能只是一个字符串。
这里是 location
作为 GeoJSON 类型的示例:
const citySchema = new mongoose.Schema({
name: String,
location: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ['Point'], // 'location.type' must be 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
}
});
Mongoose 支持 GeoJSON 对象索引,因此首先将“2dsphere”索引添加到 userCoordintes 而不是添加到对象内的坐标才能使其工作。
userCoordinates: {
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
default: undefined,
required: true
},
index: '2dsphere'
},
确保您的用户坐标看起来像这样:
const userCoordinates = {
type: "Point",
coordinates: [coordinatesA, coordinatesB],
};
我正在发送对象来创建用户模型。
"{
type: 'Point',
coordinates: [ 25.2239771, 51.4993224 ]
}"
而且,这是我创建的猫鼬模式。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserProfileSchema = new Schema(
{
userId: {
type: String,
// required: true,
unique: true,
},
userFirstName: {
type: String,
// required: true,
},
userLastName: {
type: String,
// required: true,
},
userGender: {
type: String,
// required: true,
},
userCoordinates: {
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
index: '2dsphere',
},
},
},
{ collection: 'userprofilemodels' }
);
module.exports = UserProfile = mongoose.model(
'userprofilemodels',
UserProfileSchema
);
这是我用来添加 geoJson 类型文件的代码。但是,我收到一个错误。 我还尝试在定义架构后添加索引
await new userProfileModel({
userId,
userFirstName,
userLastName,
userCoordinates,
})
.save()
.then(() => {
console.log('it worked!');
res.send('worked!');
})
.catch((error) => {
console.log('did not worl')
// console.log(error);
});
如果我排除 userCoordinates,则它有效。所以,def 我的 geoJson 对象是错误的。但是,我不知道哪里出错了。
取自猫鼬 documentation 看来 GeoJSON 类型不能只是一个字符串。
这里是 location
作为 GeoJSON 类型的示例:
const citySchema = new mongoose.Schema({
name: String,
location: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ['Point'], // 'location.type' must be 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
}
});
Mongoose 支持 GeoJSON 对象索引,因此首先将“2dsphere”索引添加到 userCoordintes 而不是添加到对象内的坐标才能使其工作。
userCoordinates: {
type: {
type: String,
default: 'Point',
},
coordinates: {
type: [Number],
default: undefined,
required: true
},
index: '2dsphere'
},
确保您的用户坐标看起来像这样:
const userCoordinates = {
type: "Point",
coordinates: [coordinatesA, coordinatesB],
};