lundi 15 février 2016

Get A Random Set of Items from MongoDb

Alright, first of all, we have a bunch of questions on this already:

One Random Record via Skip
Random Set via Skip

(SO thinks the other two links are code, I have no idea why - click edit and see references if you are interested)

My problem with all of these is that, if I understand correctly, skip is still going through all the records it skips, so it's slow. Looks like a new random method was added to mongoDb that gets a single random item from whatever matches find(). Is there some way to use this to get a set of records? Example from link...

photos.find({"author":"johndoe"}).random()

I could be completely misunderstanding. If not, would love to know how to do it and how to do it in .NET driver.

Edit: It seems like "Sample()" was added for C# Driver on Sep 18, 2015: http://ift.tt/1KlPDB6

public void Sample()
{
    var query = CreateQuery().Sample(100);

    Assert(query,
        2,
        "{ $sample: { size: 100 } }");
}

Doesn't work for me, but probably because I am running 2.0.




Aucun commentaire:

Enregistrer un commentaire