1

I have the following documents

{ _id:"myId", balance:1 }

and I want to change the field in number type to an array with the same number.

{ _id:"myId", balance: [1], }

tried

Collection.updateMany({ balance: { $type: 'number' } }, { $set: { balance: ["$balance"] } });

, didn't work ($balance string). How can i do this?

1
  • @NeNaD solution below seems best for latest versions 4.2+ , here there is some ideas on how to do in different other mongo versions: stackoverflow.com/questions/3974985/… Commented Oct 24, 2022 at 8:01

1 Answer 1

1

You have to wrap the update config with [] since you are using aggregation framework to update:

db.collection.update({
  balance: {
    $type: "number"
  }
},
[
  {
    $set: {
      balance: [
        "$balance"
      ]
    }
  }
])

Working example

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.