11

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?

2

1 Answer 1

16

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.

6
  • ah yes... i missed that bit of info. thanks for the answer! Jun 29, 2019 at 5:32
  • I have the same problem but I am using 4.2, what now? > version() 4.2.0 Aug 23, 2019 at 9:00
  • Sorry. Turns out the version was coming from the shell, not the database which is version 4.0.8 Aug 27, 2019 at 8:23
  • @SpacedMonkey so, were you able to update the version and resolve the issue? or do you still need help with something? Aug 27, 2019 at 8:37
  • @RaviShankarBharti I updated to the new version and it worked just as it should, thanks Sep 9, 2019 at 16:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.