Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb update operation with aggregation pipeline results in error code 14

Tags:

mongodb

i'm trying to update the value of a field with the value of another field of a document. mongodb docs say it's possible by using an aggregation pipeline as described here.

even the sample code from the docs results in an TypeMismatch code 14 error.

command:

db.members.update(
   { },
   [
      { $set: { status: "Modified", comments: [ "$misc1", "$misc2" ] } },
      { $unset: [ "misc1", "misc2" ] }
   ],
   { multi: true }
)

result:

WriteCommandError({
    "operationTime" : Timestamp(1561779602, 1),
    "ok" : 0,
    "errmsg" : "BSON field 'update.updates.u' is the wrong type 'array', expected type 'object'",
    "code" : 14,
    "codeName" : "TypeMismatch",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1561779602, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
})

is this an actual bug in mongodb or am i missing something?

like image 562
Dĵ ΝιΓΞΗΛψΚ Avatar asked Jun 29 '19 03:06

Dĵ ΝιΓΞΗΛψΚ


1 Answers

I think what you are facing is a mongodb version issue.

According to the official documentation :

Update with Aggregation Pipeline

Starting in MongoDB 4.2, the db.collection.update() can use an aggregation pipeline for the update. The pipeline can consist of the following stages:

  • $addFields and its alias $set
  • $project and its alias $unset
  • $replaceRoot and its alias $replaceWith.

You can see that this support is available from mongodb version 4.2, And thats why it is throwing you that error.

like image 157
Ravi Shankar Bharti Avatar answered Oct 19 '22 04:10

Ravi Shankar Bharti