How to rename a field inside an array with database commands of Azure Cosmos DB's API for MongoDB (3.6 version)?

I want to change the key of a field from name to title with database commands and NOT the shell. Below is a minified version of my collection for reference. I literally only want to change every key called name into title.

[{
  identifier: "x",
  versions: [
    {
      name: "test_name"
      version: "x.x.x"
    },
    {
      name: "test_name"
      version: "x.x.x"
    },
  ]
},
{
  identifier: "y",
  versions: [
    {
      name: "test_name2"
      version: "x.x.x"
    },
    {
      name: "test_name2"
      version: "x.x.x"
    },
  ]
}, ... ]

This query basically does exactly what I need, but it, unfortunately, isn’t supported by the newest CosmosDB MongoDB-API:

db.runCommand({
    update: 'apps',
    updates: [
        {
            q: { "versions.name": { $exists: true } },
            u: [
              {
                $set: {
                    versions: {
                        $map: {
                            input: "$versions",
                            in: {
                                $mergeObjects: [
                                    "$$this",
                                    { "title": "$$this.name" }
                                ]
                            }
                        }
                    }
                }
              },
              { $unset: "versions.name" }
            ],
            multi: true
        }
    ]
})

You can find what is supported here: https://docs.microsoft.com/de-de/azure/cosmos-db/mongodb-feature-support-36. How could this be achieved with these limitations in mind?

You can also answer this question on Stack Overflow: https://stackoverflow.com/questions/66348675/how-to-rename-a-field-inside-an-array-with-database-commands-of-azure-cosmos-db

1 Like

Hi @leaon,

As a MongoDB employee I do not know how to workaround things in Cosmos DB :slight_smile:

But I think its another proof that working with a real mongodb instance solves lots of headaches…

Is there a reason you decided to use Cosmos DB and not Atlas azure for example?

Best regards,
Pavel