[SERVER-32714] addToSet with specific fields Created: 16/Jan/18  Updated: 15/Feb/18  Resolved: 15/Feb/18

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

Type: Improvement Priority: Major - P3
Reporter: Linar Savion Assignee: Asya Kamsky
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-13841 allow optional field specified in $ad... Closed
Participants:

 Description   

Currently when updating an array with subdocuments according to a specific key, i.e. given array

[{id: 1, val: 'a'}, {id: 2, val: 'a'}, {id: 3, val: 'b'}]

, if we want to update ids 1 and 2 to new values, we would need to issue multiple update statements (which results in a blown oplog).

Adding an optional argument to addToSet to consider only specific fields would allow updating multiple subdocuments in a single call by only specifying the unique field.

This would both improve performance and reduce oplog size, and it seems like a simple addition.



 Comments   
Comment by Asya Kamsky [ 15/Feb/18 ]

I think this ticket is a duplicate of SERVER-13841.

Comment by Linar Savion [ 18/Jan/18 ]

No this does not seem like SERVER-6566 would help this issue...

If we want to insert a new set of entries into the array :

    [{'ts': ISODate("2013-10-01T01:00:00.000Z"), 'v': 999.9}, 
    {'ts': ISODate("2013-10-01T02:00:00.000Z"), 'v': 133.3},
    {'ts': ISODate("2013-10-01T03:00:00.000Z"), 'v': 223.6}, 
    {'ts': ISODate("2013-10-01T04:00:00.000Z"), 'v': 333.3},
    {'ts': ISODate("2013-10-01T05:00:00.000Z"), 'v': 423.6}]

Then the $addToSet command will only add elements if they don't exist, so the object after the update (with $addToSet considering only the 'ts' field) will look like:

{'id': 'abc', 
 'bucket': ISODate("2013-10-01T00:00:00.000Z"), 
 'values': [{'ts': ISODate("2013-10-01T01:00:00.000Z"), 'v': 123.3}, 
                {'ts': ISODate("2013-10-01T02:00:00.000Z"), 'v': 133.3},
                {'ts': ISODate("2013-10-01T03:00:00.000Z"), 'v': 223.6}, 
                {'ts': ISODate("2013-10-01T04:00:00.000Z"), 'v': 333.3},
                {'ts': ISODate("2013-10-01T05:00:00.000Z"), 'v': 423.6}
                {'ts': ISODate("2013-10-01T07:00:00.000Z"), 'v': 523.6}]}

Note that matching entries are not updated, although it could be an optional argument to override existing values...

Comment by Asya Kamsky [ 18/Jan/18 ]

If the before document is

{'id': 'abc', 
 'bucket': ISODate("2013-10-01T00:00:00.000Z"), 
 'values': [{'ts': ISODate("2013-10-01T01:00:00.000Z"), 'v': 123.3}, 
                {'ts': ISODate("2013-10-01T02:00:00.000Z"), 'v': 133.3}, ... 
                {'ts': ISODate("2013-10-01T07:00:00.000Z"), 'v': 523.6}]}

What does the after update document look like? I'm not clear on what is being added. New element to "values"?

Is this something that SERVER-6566 would address? Where if the element does not exist you want to add it but if it does exist you want to modify its contents?

Comment by Linar Savion [ 18/Jan/18 ]

Hi Asya, for example, say we have a time-series collection where each document similar to this:

{'id': 'abc', 
 'bucket': ISODate("2013-10-01T00:00:00.000Z"), 
 'values': [{'ts': ISODate("2013-10-01T01:00:00.000Z"), 'v': 123.3}, 
                {'ts': ISODate("2013-10-01T02:00:00.000Z"), 'v': 133.3}, ... 
                {'ts': ISODate("2013-10-01T07:00:00.000Z"), 'v': 523.6}]}

Then $addToSet would allow us to easily add new timestamps into the "bucket" (while enforcing no-duplicates), is think this is a pretty common use case for time-series usage of mongodb.

Currently the only way to achieve this, is using a bulk of update statements, each inserting a single timestamp if it doesn't exist (see oplog issue mentioned above). the performance benefit of using this would be substantial (both raw insert performance and writing to the journal).

Comment by Asya Kamsky [ 17/Jan/18 ]

What is the update that you want to perform on the sample array? And how does $addToSet figure into it?

Comment by Mark Agarunov [ 16/Jan/18 ]

Hello linar-jether,

Thank you for the detailed example. I've set the fixVersion to "Needs Triage" for this new feature to be scheduled against our currently planned work. Updates will be posted on this ticket as they happen.

Thanks,
Mark

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