$sample operator in MongoDB


If we want to select a certain number of documents randomly from the collection, then we have to use $ samples. A $ sample operator randomly selects a specified number of data from the input.

Syntax:


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


Note:
  1. If the size is greater than or equal to 5% of data (total documents), $sample scan the collection and sort the document.
  2. If the 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 the same document more than once in its result set.
 
Example: Suppose we have a collection named Books and wants to select 3 documents 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") }

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...