Details
-
Bug
-
Resolution: Fixed
-
Major - P3
-
4.0.10
-
Fully Compatible
-
Linux
-
v4.2
-
Query 2019-07-01
Description
db.isMaster() result:
{ "ismaster" : true, "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 100000, "localTime" : ISODate("2019-06-13T08:59:36.884Z"), "logicalSessionTimeoutMinutes" : 30, "minWireVersion" : 0, "maxWireVersion" : 7, "readOnly" : false, "ok" : 1} |
Such an operation:
db.users.aggregate([
|
{ $addFields: { id: '$_id' } }, |
{
|
$lookup: {
|
as: 'items_check', |
from: 'items', |
let: { id: '$id' }, |
pipeline: [
|
{ $addFields: { id: '$_id' } }, |
{ $match: { $expr: { $eq: ['$$id', '$owner'] } } }, |
{
|
$facet: {
|
matched: [
|
{ $match: { $or: [{ name: 'Item 2' }, { name: 'Item 1' }] }}, |
],
|
all: [{ $match: {} }],
|
},
|
},
|
],
|
},
|
},
|
{ $project: { items_check: 1 } },
|
])
|
On such collections:
{
|
|
users:[{
|
id: 'u1', |
name: 'John Smith', |
items: ['i1'], |
gadsden1: 'g1', |
gadsden2: 'g2', |
gadsden3: 'g3', |
},
|
{
|
id: 'u2', |
name: 'Jane Doe', |
items: ['i2'], |
},
|
{
|
id: 'u3', |
name: 'Christopher Gadsden', |
items: ['i3'], |
},
|
],
|
|
items:[{
|
id: 'i1', |
name: 'Item 1', |
owner: 'u1' |
},
|
{
|
id: 'i2', |
name: 'Item 2', |
owner: 'u2' |
},
|
{
|
id: 'i3', |
name: 'Item 3', |
owner: 'u3' |
},
|
],
|
|
gadsdens:[{
|
id: 'g1', |
user1: 'u1', |
user2: 'u2', |
user3: 'u3', |
},
|
{
|
id: 'g2', |
user1: 'u1', |
user2: 'u1', |
user3: 'u3', |
},
|
{
|
id: 'g3', |
user1: 'u1', |
user2: 'u2', |
user3: 'u1', |
},
|
],
|
}
|
returns such a result:
[
|
{
|
"_id": "u1", |
"items_check": [ |
{
|
"matched": [ |
{
|
"_id": "i1", |
"name": "Item 1", |
"owner": "u1", |
"id": "i1" |
},
|
{
|
"_id": "i1", |
"name": "Item 1", |
"owner": "u1", |
"id": "i1" |
}
|
],
|
"all": [ |
{
|
"_id": "i1", |
"name": "Item 1", |
"owner": "u1", |
"id": "i1" |
},
|
{
|
"_id": "i1", |
"name": "Item 1", |
"owner": "u1", |
"id": "i1" |
}
|
]
|
}
|
]
|
},
|
{
|
"_id": "u2", |
"items_check": [ |
{
|
"matched": [ |
{
|
"_id": "i2", |
"name": "Item 2", |
"owner": "u2", |
"id": "i2" |
}
|
],
|
"all": [ |
{
|
"_id": "i2", |
"name": "Item 2", |
"owner": "u2", |
"id": "i2" |
}
|
]
|
}
|
]
|
},
|
{
|
"_id": "u3", |
"items_check": [ |
{
|
"matched": [], |
"all": [ |
{
|
"_id": "i3", |
"name": "Item 3", |
"owner": "u3", |
"id": "i3" |
}
|
]
|
}
|
]
|
}
|
]
|
As we can see, the first document in the pipeline gets duplicated items. However, if we swap the $addFields and $match operators in the $lookup pipeline, everything works fine. The same goes for a situation in which the $addFields operator is removed. If I exclude the document with _id equal to u1 then the document with _id equal to u2 will get duplicated items in the items_check field. If I remove the $facet operator from the $lookup pipeline then no documents are duplicated.