-
Type: New Feature
-
Resolution: Won't Do
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Write Ops
-
Query
allow optional field specified in $addToSet operator to determine uniqueness
Several times I've wanted to use an array of sub-documents that are uniquely identified by an ID, but the other values and fields are changing or optional. Being able to use $addToSet versus querying and updating or updating and querying would be a big win in performance.
A couple examples:
Array of members with optional 'name'.
{ _id: '9e2eea2c-d36e-48a3-aeed-882c737447b6' members: [ { id: 'af39c730-928b-4d3a-acfe-6db682325a54', name: 'Fred' }, { id: '39593884-ec1a-44a9-a720-b6b797cf4fea', name: null } ] }
I would like to be able to call update with $addToSet on the members array, without habing to worry about the presence of a 'name' determing uniqueness.
db.collection.update({_id: '9e2eea2c-d36e-48a3-aeed-882c737447b6'},{$addToSet: {members: {id: '39593884-ec1a-44a9-a720-b6b797cf4fea', name: 'Mary'}})
Could have an optional operator to determine uniqueness:
db.collection.update({_id: '9e2eea2c-d36e-48a3-aeed-882c737447b6'},{$addToSet: {members: {id: '39593884-ec1a-44a9-a720-b6b797cf4fea', name: 'Mary'}, $unique: id})
Another example is similar but even more of an issue for me. I would like to be able to use $inc on fields within the sub-document in the array using $addToSet in a single operation.
{ _id: '9e2eea2c-d36e-48a3-aeed-882c737447b6' counts: [ { id: 'af39c730-928b-4d3a-acfe-6db682325a54', count: 1 }, { id: '39593884-ec1a-44a9-a720-b6b797cf4fea', count: 5 } ] }
db.collection.findAndModify({_id: '9e2eea2c-d36e-48a3-aeed-882c737447b6'}, {}, {$addToSet: {id: 'af39c730-928b-4d3a-acfe-6db682325a54', $inc: {count: 1}, {new: true}
count for 'af39c730-928b-4d3a-acfe-6db682325a54' would be 2.
db.collection.findAndModify({_id: '9e2eea2c-d36e-48a3-aeed-882c737447b6'}, {}, {$addToSet: {id: '828e8625-65c5-407e-a5cf-0e20d3698eaa', $inc: {count: 1}, {new: truae}
count for '828e8625-65c5-407e-a5cf-0e20d3698eaa' would be 1.
- is duplicated by
-
SERVER-32714 addToSet with specific fields
- Closed