Home > Net >  Convert (update) number to array mongoDB
Convert (update) number to array mongoDB

Time:10-24

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?

CodePudding user response:

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

  • Related