Rahman
Rahman

Reputation: 452

How to convert a type of a string field to array in MongoDB

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?

Upvotes: 0

Views: 506

Answers (1)

nimrod serok
nimrod serok

Reputation: 16033

You can use an update pipeline:

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

See how it works on the playground example

Upvotes: 1

Related Questions