0

I want to create a new location field from already existing longitude and latitude.

db.neigborhood.updateMany({}, {
$set: {
    "location": {
        "type": "Point",
        "coordinates": ["$longitude", "$latitude"]
    }   
}});

I wrote this code that should create the new field, but the problem is that instead of the field values I get the names as strings.

{
  "_id": {
    "$oid": "626a01f1df85b4b2937ece2d"
  },
  "latitude": "10.4980067",
  "longitude": "-66.8335096",
  "location": {
    "type": "Point",
    "coordinates": [
      "$longitude",
      "$latitude"
    ]
  }
}

What am I doing wrong that I get "$longitude" instead of the -66.8335096 value?

1 Answer 1

2

Works with Update with Aggregation Pipeline.

db.neigborhood.updateMany({},
[
  {
    $set: {
      "location": {
        "type": "Point",
        "coordinates": [
          "$longitude",
          "$latitude"
        ]
      }
    }
  }
])

Sample Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.