Home > Enterprise >  How to convert a type of a string field to array in MongoDB
How to convert a type of a string field to array in MongoDB

Time:10-07

I have a field in a document whose type is string, like below:

{
_id:ObjectId("632c12d98b4d9347774a4e9f"),
keyword:'keyword1'
}

I want to change it to below:

{
_id:ObjectId("632c12d98b4d9347774a4e9f"),
keyword:['keyword1']
}

How can I do it in MongoDB?

CodePudding user response:

You can use an update pipeline:

db.collection.update({},
[
  {$set: {keyword: ["$keyword"]}}
])

See how it works on the playground example

  • Related