$push and $sort: A couple separated by $slice


#1

$sort is a great mongo’s modifier!

The $sort modifier orders the elements of an array during a $push operation.

According to the documentation, setting $slice is not one of its requirements. Minimongo, however, requires $slice to be set as a zero or negative number.

I checked Meteor’s code trying to understand why, but I cannot find the reasons. Could someone help me on that?


I could workaround it by setting $slice as the opposite of

(destination list).length + (items being added).length

but I would risk losing data in case of a race condition.


#2

Related issue: Minimongo Error on Serverside Meteor Script?


#3

It is minimongo - it just emulate mongo, so it has nothing to do with the real mongo methods.
you can sort array by JS and insert it to minimongo in the exact state as you want it
or you can call real meteor.method to do it on server side for real mongo driver.


#4

I got it. Still, does minimongo need $slice? For what reason?


#5

Check sources if there is some hint… :smiley: