Say I have a document that contains an array named Months. Each element in the array has a int field indicating which month & year it refers to (yyyyMM), e.g. 201612, 201701, etc.
Each such element also contains additional fields, pertaining to that particular month. e.g. int numOfItems, string Country, etc.
Now, assuming I have such document with only 2 elements in the array:
Months[0].month == 201612
Months[1].month == 201701
Then, I call addToSet with Months[1].month == 201701.
How does addToSet determine whether this is a duplicate or not?
Does it inspect and compare all sub-elements of Months[1] to do that? Does it only inspect the index of the element in the Months array?
This is directly from MongoDB.
If the value is a document, MongoDB determines that the document is a duplicate if an existing document in the array matches the to-be-added document exactly; i.e. the existing document has the exact same fields and values and the fields are in the same order. As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.