updateMany with aggregation pipeline

In the JavaScript Web API to Atlas App Services I’d like to use updateMany with an aggregation pipeline, but am getting the error: cannot transform type primitive.D to a BSON Document: WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel.

Is the aggregation pipeline supported within updateMany? I’m a bit confused by which documentation I should be looking at.

If it’s not supported, do I have any options for conditionally updating multiple documents without looping through on the client side, reading each document, testing the condition, then updating?

Hey @Daniel_Bernasconi,

The aggregation pipeline within updateMany() is fully supported from MongoDB 4.2 onwards. As stated in the MongoDB documentation, the <update> argument must be an array of one or more pipeline stages.

db.collection.updateMany(
   <filter>,
   [ <update> ] // aggregation pipeline
)

If you are encountering an issue, please make sure you are running MongoDB server version 4.2 or later, and everything is correct in your pipeline stages.

As for the error that cannot transform type primitive.D to a BSON Document, it appears your update clause may not be formatted correctly. Could you please share more details about your update clause so we can assist accordingly? In addition to these, please also share the sample documents and the expected output.

Regards,
Kushagra

Thanks Kushagra. I’m using the hosted Atlas App Services (cloud.mongodb.com) and the version is 6.0.9.

The code that is generating that error is as follow:

        // Error
        taskCollection.updateMany(
            { sectionId : sId },
            [{ $set: { rows: {
                $add: [
                    "$rows",
                    { $subtract: [
                        { $min: [ 7, { $add: [ "$rowStart", "$rows", -1 ] } ] },
                        { $max: [ 3, { $add: [ "$rowStart", 1 ] } ] }
                    ]},
                ],
            }}}],
        );

However, I get the same erorr with this:

        // Error
        taskCollection.updateMany(
            { },
            [{$set: { rows : 3 }}],
        );

I get no error if I do a $set without aggretation:

        // No error
        taskCollection.updateMany(
            { },
            {$set: { rows : 3 }},
        );

Hey @Daniel_Bernasconi,

Could you confirm if you are talking about Atlas Functions?

If yes, could you share the whole code snippet and the exact error message you are encountering?

Regards,
Kushagra

Hi @Kushagra_Kesav,

No, this is not an Atlas Function. This is a JavaScript code that I’m running within a React app within a browser on a PC, using the Realm-Web API.

I’ve got numerous database API calls that work fine, and have been developing the application for a few months, but this is the first time I’ve tried to use aggregation in an updateMany call.

The code is as in my previous post, and the error I’m getting is: “cannot transform type primitive.D to a BSON Document: WriteArray can only write a Array”

Thanks