12

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
  • Can you tell me your mongodb version? Commented Jun 29, 2019 at 5:24
  • @RaviShankarBharti server is 4.0.6 Commented Jun 29, 2019 at 5:28

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.

Sign up to request clarification or add additional context in comments.

6 Comments

ah yes... i missed that bit of info. thanks for the answer!
I have the same problem but I am using 4.2, what now? > version() 4.2.0
Sorry. Turns out the version was coming from the shell, not the database which is version 4.0.8
@SpacedMonkey so, were you able to update the version and resolve the issue? or do you still need help with something?
@RaviShankarBharti I updated to the new version and it worked just as it should, thanks
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.