1

How I can change the name of the embed type by query $rename?

{ list_name: [{ name_1: String }] } => { list_name: [{ name_2: String }] }

I tried db.getCollection('test').updateMany({}, { $rename: { 'list_name.name_1': 'name_2' }})

But it's throwing an error: WriteError: cannot use the part (list_name of list_name.name_1) to traverse the element ({list_name: [ { name_1: "test" } ]})

Vũ Anh Dũng
  • 980
  • 3
  • 13
  • 32

1 Answers1

0

$rename does not work if these fields are in array elements

Refer

To achieve, you need $unset and $set document by document. You can use bulkwrite also.

list_name is an array. Hence $rename didn't work.

When I search, I get this. You can refer the second answer if you have less number of docs.

Else, you need to use either bulk write or mongo dump options.

Gibbs
  • 21,904
  • 13
  • 74
  • 138