Muirik
Muirik

Reputation: 6289

Renaming Property Within Array in MongoDB Collection

I am wanting to rename a property in one of our MongoDB collections because it was pluralized where it should be singular. However, when I try this in the Mongo shell:

db.getCollection("customers").updateMany( { _id: {$exists: true} }, { $rename: { "billingOptions.0.paymentCards": "billingOptions.0.paymentCard" } } )

... I get this "cannot be an array element" error:

"errmsg" : "The source field cannot be an array element, 'billingOptions.0.paymentCards' in doc with _id: ObjectId('3d12fefc093r76146ccf50g8') has an array field called 'billingOptions'"

Not sure why this would be a problem, since I'm telling it precisely which array element to target. But, either way, this being the case, what operation can I use to rename this property

Here's an example of the relevant section of the document as it is now:

"billingOptions" : [
    {
        "method" : "private", 
        "paymentCards": {
            "deleted" : false,
            // other props
        }
    }, 
]

And this is what I'd like it to look like after:

"billingOptions" : [
    {
        "method" : "private", 
        "paymentCard": {
            "deleted" : false,
            // other props
        }
    }, 
]

Note that "billingOptions" is a property on the root of the document. All I want to do is rename all instances of "paymentCards" to "paymentCard", since it is a singular object here, rather than an array.

Upvotes: 1

Views: 411

Answers (1)

mickl
mickl

Reputation: 49945

You need $out to replace existing collection and $addFields with $map to replace existing array in each document

db.customers.aggregate([
    {
        $addFields: {
            billingOptions: {
                $map: {
                    input: "$billingOptions",
                    in: {
                        method: "$$this.method",
                        // other fields
                        paymentCard: "$$this.paymentCards"
                    }
                }
            }
        }
    },
    {
        $out: "$customers"
    }
])

Upvotes: 2

Related Questions