MongoDB: atomically pop random element -


There is no way on which temporarily populate a random element of the Mongodebi - like radis (remove and retrieve) pop ?

I have read, but now I need to make sure that the element is removed even when it is received, and it should be done atomically.

I think that as an alternative I was able to push data into an array before the field was sorted, but I really like to get it a random record. Looking at the document of

$ pop , it seems that it can not be argued, so it either removes the first or the last element of an array.

If there are atoms updated within the collection in Mongo, then with a randomly approach approach findAndModify combination:

  var rand = Math.rand (); Db.collection.findAndModify ({query: {random: {$ gte: rand}}, remove: true});   

Just make sure to query for random: {$ lt: rand} when returns above null .

PRIPIP: Some drivers have findAndRemove , which remove with findAndModify : true .

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -