1

Given a collection of MongoDB documents where each document have an array of numbers stored as strings. How do i modify all documents to store the element as numbers instead of strings?

Currently, the documents are structured like this:

{
"coordinates": ["7.83", "58.08"]
}

The desired output would be like this

{
"coordinates": [7.83, 58.08]
}

The collection have 1M+ documents, and solutions that involves processing the documents in code, would not be desirable.

I have tried to use the $toDouble operator combined with $updateMany, without success.

1 Answer 1

2
db.collection.update({},
[ //Aggregate update
  {
    "$set": {
      "coordinates": {
        $map: {
          input: "$coordinates",
          in: {
            $toDouble: "$$this" //Aggregate operators required to convert
          }
        }
      }
    }
  }
],
{
  "multi": true,
  "upsert": false
})

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.