如何以正确的 geoJSON 格式存储
How to store in correct geoJSON format
我正在尝试按如下方式以 geoJSON 格式存储一些数据。
var newUser = {
'username': $('#addUser fieldset input#inputUserName').val(),
'location': {type: "Point", coordinates: [ $('#addUser fieldset input#inputUserLocationLng').val(), $('#addUser fieldset input#inputUserLocationLat').val()]},
'email': $('#addUser fieldset input#inputUserEmail').val(),
'phone': $('#addUser fieldset input#inputUserPhone').val(),
'chat': $('#addUser fieldset select#inputChatAllowed').val()
}
并保存如下图
{
"_id" : ObjectId("5e327c7b8c0181090e15d445"),
"username" : "test2",
"location[type]" : "Point",
"location[coordinates][]" : [
77.641145,
12.89149
],
"email" : "test2@gmail.com",
"phone" : "8998778987",
"chat" : "0"
}
但我希望位置部分采用经过验证的正确 geoJSON 格式 here 并如下所示。
"location": {
"type" : "Point",
"coordinates" : [
-73.856077,
40.848447
]
}
我正在使用 mongoose 和 nodejs。任何建议都会有所帮助。
此外,mongoDB 中的 geoJSON 数据文档是否也可以包含非位置数据(姓名、联系人)?
文档看起来很奇怪。也许这将是一个解决方法:
doc = db.col.insertOne({
"username": "test2",
"email": "test2@gmail.com",
"phone": "8998778987",
"chat": "0"
})
db.col.updateOne(
{ _id: doc.insertedId },
{ $set: { "location.type": "Point" } }
)
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": { $each: [-73.856077, 40.848447] } } }
)
或
db.col.updateOne(
{ _id: doc.insertedId },
{
$set: {
"location": {
type: "Point",
coordinates: [-73.856077, 40.848447]
}
}
}
)
或
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": -73.856077 } }
)
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": 40.848447 } }
)
结果:
{
"_id" : ObjectId("5e32eb3405a39c3341179e7f"),
"username" : "test2",
"email" : "test2@gmail.com",
"phone" : "8998778987",
"chat" : "0",
"location" : {
"type" : "Point",
"coordinates" : [
-73.856077,
40.848447
]
}
}
我正在尝试按如下方式以 geoJSON 格式存储一些数据。
var newUser = {
'username': $('#addUser fieldset input#inputUserName').val(),
'location': {type: "Point", coordinates: [ $('#addUser fieldset input#inputUserLocationLng').val(), $('#addUser fieldset input#inputUserLocationLat').val()]},
'email': $('#addUser fieldset input#inputUserEmail').val(),
'phone': $('#addUser fieldset input#inputUserPhone').val(),
'chat': $('#addUser fieldset select#inputChatAllowed').val()
}
并保存如下图
{
"_id" : ObjectId("5e327c7b8c0181090e15d445"),
"username" : "test2",
"location[type]" : "Point",
"location[coordinates][]" : [
77.641145,
12.89149
],
"email" : "test2@gmail.com",
"phone" : "8998778987",
"chat" : "0"
}
但我希望位置部分采用经过验证的正确 geoJSON 格式 here 并如下所示。
"location": {
"type" : "Point",
"coordinates" : [
-73.856077,
40.848447
]
}
我正在使用 mongoose 和 nodejs。任何建议都会有所帮助。
此外,mongoDB 中的 geoJSON 数据文档是否也可以包含非位置数据(姓名、联系人)?
文档看起来很奇怪。也许这将是一个解决方法:
doc = db.col.insertOne({
"username": "test2",
"email": "test2@gmail.com",
"phone": "8998778987",
"chat": "0"
})
db.col.updateOne(
{ _id: doc.insertedId },
{ $set: { "location.type": "Point" } }
)
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": { $each: [-73.856077, 40.848447] } } }
)
或
db.col.updateOne(
{ _id: doc.insertedId },
{
$set: {
"location": {
type: "Point",
coordinates: [-73.856077, 40.848447]
}
}
}
)
或
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": -73.856077 } }
)
db.col.updateOne(
{ _id: doc.insertedId },
{ $push: { "location.coordinates": 40.848447 } }
)
结果:
{
"_id" : ObjectId("5e32eb3405a39c3341179e7f"),
"username" : "test2",
"email" : "test2@gmail.com",
"phone" : "8998778987",
"chat" : "0",
"location" : {
"type" : "Point",
"coordinates" : [
-73.856077,
40.848447
]
}
}