[SERVER-8008] $push to sorted array Created: 21/Dec/12  Updated: 18/Sep/15  Resolved: 27/Dec/12

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

Type: New Feature Priority: Minor - P4
Reporter: Yuri Finkelstein Assignee: Alberto Lerner
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Related
related to SERVER-991 $push with $slice + $sort Closed
related to CSHARP-684 Add new features to Update builder fo... Closed
Participants:

 Description   

Now that SERVER-991 is resolved, more can be accomplished with this if Mongo would maintain arrays sorted by specified field.

Let's say the initial document state was:

{a: [{version: 1, data: 10}]}

After

{ $push : { a : { $each : [ { version : 3, data : 6 } ] , $orderBy : {version : -1 }, $trimTo : 11 } } }

it will be

{a: [{version: 2, data: 6}, {version: 1, data: 10}]}

After

{ $push : { a : { $each : [ { version : 2, data : 8 } ] , $orderBy : {version : -1 }, $trimTo : 11 } } }

it will be

{a: [{version: 3, data: 6}, {version: 2, data: 8}, {version: 1, data: 10}]}

In this case,
Mongo maintains array in "a" sorted by version in discending order and
trims the array to 11 elements by dropping the very last element when
required.

The context here is that this mechanism would enable capability similar to
http://hbase.apache.org/book/versions.html



 Comments   
Comment by Githook User [ 11/Mar/15 ]

Author:

{u'username': u'kaloianm', u'name': u'Kaloian Manassiev', u'email': u'kaloian.manassiev@mongodb.com'}

Message: Revert "SERVER-8008: $unwind of scalar should return 1 doc with scalar"

This reverts commit 25feddadeef43fb0668350b40282fcdfbb1f2296.
Branch: master
https://github.com/mongodb/mongo/commit/5ad411b38b020b209218017448426e9dc72382b5

Comment by Yuri Finkelstein [ 20/Mar/13 ]

What's g+? Google plus? I barely use it. I can talk about this at MongoSF later this year.

Comment by Alberto Lerner [ 20/Mar/13 ]

Yuri, when can we read your g+ post about those usage cases?

Comment by Yuri Finkelstein [ 19/Mar/13 ]

Thanks for making this happen. This is a great feature, but perhaps it would require a bit more effort to explain folks what can be done with it. This is in fact a perfect instrument for MVCC-type writes against Mongo. Also, many messaging systems do not guarantee in-order delivery. So, if a user app is storing data to mongo in response to message receipt but needs to order the result in some way (by timestamp, sequence counter, etc) it now can do it with this instrument. Ability to trim the array is paramount to avoid unbounded document growth. I think you need to publicize what these interesting potential applications of this feature.

Comment by auto [ 15/Jan/13 ]

Author:

{u'date': u'2013-01-15T22:53:57Z', u'email': u'alerner@10gen.com', u'name': u'Alberto Lerner'}

Message: SERVER-8008 Fixed uassert numbers.
Branch: master
https://github.com/mongodb/mongo/commit/09139c400d9fe5a6a0d19764395f22b4333e761f

Comment by auto [ 15/Jan/13 ]

Author:

{u'date': u'2013-01-14T04:16:36Z', u'email': u'alerner@10gen.com', u'name': u'Alberto Lerner'}

Message: SERVER-8008 Moved checks for sort pattern to early and performed them only once per update.
Branch: master
https://github.com/mongodb/mongo/commit/503672094dcc6370bc015638cc36c0a306cf2996

Comment by auto [ 03/Jan/13 ]

Author:

{u'date': u'2013-01-03T21:23:09Z', u'name': u'Alberto Lerner', u'email': u'alerner@10gen.com'}

Message: SERVER-991 SERVER-8008 Fixed test to use $slice instead of $trimTo.
Branch: master
https://github.com/mongodb/mongo/commit/56090cce56cc8e700b8dedc809e50523d3272cea

Comment by auto [ 03/Jan/13 ]

Author:

{u'date': u'2013-01-03T17:19:13Z', u'name': u'Alberto Lerner', u'email': u'alerner@10gen.com'}

Message: SERVER-991 SERVER-8008 Replaced $trimTo by $slice (but only accepting negative slices for now).
Branch: master
https://github.com/mongodb/mongo/commit/b3687e73545f6d0d04f1992119308e7ab80d44e1

Comment by auto [ 27/Dec/12 ]

Author:

{u'date': u'2012-12-27T05:47:19Z', u'email': u'alerner@10gen.com', u'name': u'Alberto Lerner'}

Message: SERVER-8008 Corrected types (fixes 32-bit compilation).
Branch: master
https://github.com/mongodb/mongo/commit/7b27beab091addc90e058d7342d0db360f271c62

Comment by auto [ 27/Dec/12 ]

Author:

{u'date': u'2012-12-27T04:58:05Z', u'email': u'alerner@10gen.com', u'name': u'Alberto Lerner'}

Message: SERVER-8008 Adjusted error codes (fixes compilation).
Branch: master
https://github.com/mongodb/mongo/commit/9a9a2a35cf794189fc2ebd8fa772ed37e1eed329

Comment by Alberto Lerner [ 27/Dec/12 ]

Author:

{u'date': u'2012-12-27T02:23:22Z', u'email': u'alerner@10gen.com'}

Message: SERVER-8008 Allowed to sort the result of a $push array when using $trimTo.
Branch: master
https://github.com/mongodb/mongo/commit/e561ddfb5a5fba96741138e4f4125be126ba25bd

Comment by Yuri Finkelstein [ 21/Dec/12 ]

I cloned SERVER-991 and can not change the original description of the ticket. Can somebody do this please.

Comment by Yuri Finkelstein [ 21/Dec/12 ]

Now that SERVER-991 is resolved, more can be accomplished with this if Mongo would maintain arrays sorted by specified field.

Let's say the initial document state was:

{a: [{version: 1, data: 10}]}

After

{ $push : { a : { $each : [ { version : 3, data : 6 } ] , $orderBy : {version : -1 }, $trimTo : 11 } } }

it will be

{a: [{version: 2, data: 6}, {version: 1, data: 10}]}

After

{ $push : { a : { $each : [ { version : 2, data : 8 } ] , $orderBy : {version : -1 }, $trimTo : 11 } } }

it will be

{a: [{version: 3, data: 6}, {version: 2, data: 8}, {version: 1, data: 10}]}

In this case,
Mongo maintains array in "a" sorted by version in discending order and
trims the array to 11 elements by dropping the very last element when
required.

The context here is that this mechanism would enable capability similar to
http://hbase.apache.org/book/versions.html

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