Details
-
Question
-
Resolution: Unresolved
-
Major - P3
-
None
-
None
-
None
-
None
-
QE 2021-08-23
Description
I have some sample data as follows:
> db.first.find()
|
{ "_id" : 1, "flavor" : "chocolate" } |
|
|
|
|
> db.another.find()
|
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 } |
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 } |
{ "_id" : 3 } |
{ "_id" : 4, "item" : "ginger" } |
When running a pipeline with $merge:
db.first.aggregate([
|
{
|
$merge: {
|
"into": "another", |
"whenMatched": [ |
{
|
"$project": { |
"_id": 0 |
}
|
}
|
]
|
}
|
}
|
])
|
This results in no error, and no updates to the "another" collection. Is this expected? I assumed this would not be allowed as there is a restriction on the "whenMatched" pipeline not updating the "on" field.
Another example, using a different "on" field:
> db.second.find()
|
{ "_id" : ObjectId("60dcae7e4a59a35f0ed6b34b"), "item" : "almonds" } |
|
|
|
|
> db.another.find()
|
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 } |
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 } |
{ "_id" : 3 } |
{ "_id" : 4, "item" : "ginger" } |
Pipeline:
db.second.aggregate([
|
{
|
$merge: {
|
"into": "another", |
"whenMatched": [ |
{
|
"$project": { |
"item": 0 |
}
|
}
|
]
|
}
|
}
|
])
|
uncaught exception: Error: command failed: {
|
"ok" : 0, |
"errmsg" : "E11000 duplicate key error collection: playground.another index: item_1 dup key: { item: \"almonds\" }", |
"code" : 11000, |
"codeName" : "DuplicateKey", |
"keyPattern" : { |
"item" : 1 |
},
|
"keyValue" : { |
"item" : "almonds" |
}
|
} : aggregate failed :
|
_getErrorWithCode@src/mongo/shell/utils.js:25:13 |
doassert@src/mongo/shell/assert.js:18:14 |
_assertCommandWorked@src/mongo/shell/assert.js:618:17 |
assert.commandWorked@src/mongo/shell/assert.js:708:16 |
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 |
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 |
@(shell):1:1 |
It seems that in this case I run into a duplicate key error as we attempt to insert a results document. What is the correct behavior in this case?