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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With