Jun 30, 2016

$sample operator in MongoDB

If we want select document randomly in fixed number of document then we have to use $sample. $sample operator randomly selects specific number of data from input.

Syntax:


{ $sample: { size: <positive integer> } }


Note:
  1. If size is greater than or equal to 5% of data (total documents), $sample scan the collection and sort the document.
  2. If size is less than 5% of data (total documents) then $sample use the pseudo random cursor on the collection if using WiredTiger Storage Engine.
  3. $sample use the _id to select random data if using MMAPv1 Storage Engine.
  4. $sample can select same document more than once in its result set.
 
Example: Suppose we have a collection named Books and wants to select 3 document randomly.


{ "_id" : 1, "Book Title": "book1", "price" : 20, "quantity" : 1, "date" : ISODate("2016-08-05T07:00:00Z") }
{ "_id" : 2, " Book Title " : " book2", "price" : 10, "quantity" : 2, "date" : ISODate("2016-08-05T08:00:00Z") }
{ "_id" : 3, " Book Title " : " book3", "price" : 30, "quantity" : 4, "date" : ISODate("2016-08-17T10:00:00Z") }
{ "_id" : 4, " Book Title " : " book4", "price" : 10, "quantity" : 2, "date" : ISODate("2014-09-01T11:20:39.736Z") }
{ "_id" : 5, " Book Title " : " book5", "price" : 20, "quantity" : 6, "date" : ISODate("2014-09-04T20:23:13.331Z") }


Run following query


db.Books.aggregate({ $sample: { size: 3 } })


Result:


{ "_id" : 5, " Book Title " : " book5", "price" : 20, "quantity" : 6, "date" : ISODate("2014-09-04T20:23:13.331Z") }
{ "_id" : 1, "Book Title" : "book1", "price" : 20, "quantity" : 1, "date" : ISODate("2016-08-05T07:00:00.000Z") }
{ "_id" : 3, " Book Title " : " book3", "price" : 30, "quantity" : 4, "date" : ISODate("2016-08-17T10:00:00.000Z") }