[SERVER-13841] allow optional field specified in $addToSet operator to determine uniqueness Created: 06/May/14  Updated: 03/Apr/23  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Zach Davis Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 9
Labels: transactions
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-32714 addToSet with specific fields Closed
Related
Assigned Teams:
Query
Participants:

 Description   

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.



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

This can be done by setting the array to its new value using various aggregation array expressions.

Comment by Roger Bush [ 22/Jan/17 ]

A simple and useful uniqueness definition would provide the ability to specify a list of fields which are relevant in an equality/uniqueness comparison. If no fields are specified, then you'd get the default behavior (all fields are used to determine equality).

This feature alone would simplify many update/insert scenarios that currently take 2 operations. The problem with using 2 operations is it introduces potential distributed race conditions.

Use of this feature with a simple id almost completely eliminates any benefits of using objects versus arrays (MongoDB's feature set preferences arrays, allowing the contents to be indexed - there are also many array related features. Objects are not as supported). For example, by using the id field as the sole equality determining field, we can do an $addToSet, and in a single operation insert a unique element by id (much like what you get in a javascript object/associative array), as well as constructing the array in the initialization case. This can all be done in a single command.

This would make a lot of operations more straightforward.

Comment by Víctor Herraiz Posada [ 10/Nov/15 ]

It should be nice to have this feature and be able to change the way "$addToSet" determines uniqueness using a field restriction or a compare function.

Comment by Sajin [ 25/Jun/14 ]

As Op mentioned several fields of objects in would be optional / timestamp field etc.
Above requested feature will be great impact on queries.

Generated at Thu Feb 08 03:33:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.