Как хранить в правильном формате Geo JSON - PullRequest
1 голос
/ 30 января 2020

Я пытаюсь сохранить некоторые данные в формате geo JSON следующим образом.

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"
}

Но я хочу местоположение деталь в правильном гео JSON Отформатируйте как проверено здесь и показано ниже.

"location": { 
        "type" : "Point", 
        "coordinates" : [
            -73.856077, 
            40.848447
        ]
    }

Я использую mon goose и nodejs. Любые предложения будут полезны.

Кроме того, может ли документ данных geo JSON в mongoDB содержать также данные о местоположении (имя, контакт)?

1 Ответ

1 голос
/ 30 января 2020

Документ выглядит странно. Возможно, это будет обходной путь:

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
        ]
    }
}
...