What is $sample in MongoDB and Why Can it be Extremely Useful?

What is the $sample operator in MongoDB? Explained with the help of a lucky draw winner application.

Tek Loon
JavaScript in Plain English

--

Photo by René Porter on Unsplash

Welcome back to another story. I will be sharing about the $sample operator in MongoDB in this story. The story consists of 4 parts:

  • What is the $sample operator?
  • A practical use case where we leverage the $sample operator.
  • Example code.
  • Helpful resources for the topic.

Without further ado, let’s get started.

What is the $sample operator?

$sample is a MongoDB operator that we can use in the aggregation pipeline. As you may have guessed, it gives you a sample of data — which means a random subset of data.

You can relate the above to a real-life situation — food promoters in the mall. What food promoters do is separate the food into multiple portions. Every single portion is known as a food sample. Consumers like you get to taste a sample of the whole food.

The below screenshot is what the syntax of the $sample operator looks like.

Syntax for MongoDB $sample operator

A practical use case where we can leverage the $sample operator

Now that we have a clear understanding of what the $sample operator is able to do, the next question is probably why and when do you need to use it. I am glad that you asked this question.

Lucky Draw & Giveaway Winner Application

One of the most popular use cases is a lucky draw winner scenario. This use case is very practical. Regardless of whether you come from an enterprise, small business, or startup, you would definitely run promotions such as giveaways, lucky draws, or promotional campaigns.

Building a lucky draw winner query could be pretty troublesome. The traditional approach that we normally implement looks like this:

  • Count the total number of records from the table (SQL) or collection (MongoDB).
  • Generate a random number between 0 and the total number of records.
  • Then, we use the generated random number as the index and retrieve the desired data.

The traditional approach above is assuming you’re selecting only one winner. However, if you’re selecting the top 30 winners with random sequences. That could really get complicated.

But the $sample operator does all the hard work above and we only need to write a single or few lines of code to achieve it. And that is just amazing and elegant.

More powerful when used with other operators

$sample is more powerful when used with another operator such as $match. The $match operator allows you to filter data based on conditions.

A very good example is you wouldn’t want the lucky draw winner to be an inactive user who never supports your company's products. You would love the lucky draw winner to be one of your loyal customers who trusted and supported your company along the journey.

Thus, using the $match operator with the $sample operator is a great combination here.

Example Code

The above code shows that we:

  • Filter the users with active profile status and platinum level.
  • Then, among all the active and platinum users, we choose only 1 user as the winner.

The code above looks clean and easy to understand compared with the traditional approach. I highly recommend you to use $sample if you’re using MongoDB.

Conclusion

In this story, I shared:

  • What is the $sample operator? And related it with food promoters.
  • Practical use case using $sample — lucky draw winner application.
  • Lastly, an example of code using $sample— clean and easy to understand.

Thanks for reading and feel free to share with me your use cases in leveraging the $sample operator. Looking forward to hearing from you.

More content at plainenglish.io

--

--

Coder and Writer. If you enjoy my stories— support me by https://www.buymeacoffee.com/tekloon so I can keep writing articles for the community.