-
Notifications
You must be signed in to change notification settings - Fork 1.7k
DOCS-13883 add random number generator #4773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andf-mongodb
merged 1 commit into
mongodb:master
from
mungitoperrito:DOCS-13883-add-rand-number-generator-v4.4.2
Jan 12, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| =================== | ||
| $rand (aggregation) | ||
| =================== | ||
|
|
||
| .. default-domain:: mongodb | ||
|
|
||
| .. contents:: On this page | ||
| :local: | ||
| :backlinks: none | ||
| :depth: 1 | ||
| :class: singlecol | ||
|
|
||
| Definition | ||
| ---------- | ||
|
|
||
| .. expression:: $rand | ||
|
|
||
| .. versionadded:: 4.4.2 | ||
|
|
||
| Returns a random float between 0 and 1 each time it is called. | ||
|
|
||
| :expression:`$rand` has the following syntax: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| { $rand: {} } | ||
|
|
||
| The :expression:`$rand` operator doesn't take any arguments. | ||
|
|
||
| Behavior | ||
| -------- | ||
| Each time ``$rand`` is called it will return a floating point value | ||
| that has up to 17 digits after the decimal point. Trailing 0s are | ||
| dropped so the actual number of digits may vary. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| This code initializes a ``randomSamples`` collection with 100 documents | ||
| that is used in the following examples. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| N = 100 | ||
| bulk = db.randomSamples.initializeUnorderedBulkOp() | ||
| for ( i = 0; i < N; i++) { bulk.insert( {_id: i, random: 0 } ) } | ||
| bulk.execute() | ||
|
|
||
|
|
||
| Usage with Update Queries | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The ``$rand`` operator can be used with update query operations. In | ||
| this example :method:`~db.collection.updateMany()` uses the ``$rand`` | ||
| operator to insert a different random number into each document | ||
| in the ``randomSamples`` collection. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.randomSamples.updateMany( | ||
| {}, | ||
| [ | ||
| { $set: { "random": { $rand: {} } } } | ||
| ] | ||
| ) | ||
|
|
||
| We can use :pipeline:`$project` to see the output. The | ||
| :pipeline:`$limit` stage halts the pipeline after the third document. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.randomSamples.aggregate( | ||
| [ | ||
| { $project: {_id: 0, random: 1 } }, | ||
| { $limit: 3 } | ||
| ] | ||
| ) | ||
|
|
||
| The output shows the random values. | ||
|
|
||
| .. code-block:: javascript | ||
| :copyable: false | ||
|
|
||
| { "random" : 0.8751284485870464 } | ||
| { "random" : 0.515147067802108 } | ||
| { "random" : 0.3750004525681561 } | ||
|
|
||
| Rounding to Control the Number of Output Digits | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| If you want a shorter random value, consider using :expression:`$round`. | ||
| Note that the :pipeline:`$set` stage updates the document, if ``$rand`` | ||
| is called in a :pipeline:`$project` stage the underlying document is | ||
| not modified. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.randomSamples.aggregate( | ||
| [ | ||
| { $match: {} }, | ||
| { $set: { rounded: { $round: [ "$random", 4 ] } } }, | ||
| { $out: "randomSamples" } | ||
| ] | ||
| ) | ||
|
|
||
| The :pipeline:`$project` stage displays the original and rounded value | ||
| for each document. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.randomSamples.aggregate( | ||
| [ | ||
| { $project: {_id:0, random:1, rounded: 1 } }, | ||
| { $limit: 3 } | ||
| ] | ||
| ) | ||
|
|
||
| The update documents look like this: | ||
|
|
||
| .. code-block:: javascript | ||
| :copyable: false | ||
|
|
||
| { "random" : 0.8751284485870464, "rounded" : 0.8751 } | ||
| { "random" : 0.515147067802108, "rounded" : 0.5151 } | ||
| { "random" : 0.3750004525681561, "rounded" : 0.375 } | ||
|
|
||
| .. note:: | ||
|
|
||
| Like ``$rand``, the value returned by the ``$round`` operator does | ||
| not include any trailing 0s so the number of digits returned may | ||
| vary. | ||
|
|
||
| Selecting Random Items From a Collection | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| The ``$rand`` operator can be used in an aggregation pipeline to select | ||
| random documents from a collection. In this example we use ``$rand`` to | ||
| select about half the documents in the ``randomSamples`` collection. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.randomSamples.aggregate( | ||
| [ | ||
| { $match: { $expr: { $lt: [0.5, {$rand: {} } ] } } }, | ||
| { $count: "numMatches" } | ||
| ] | ||
| ) | ||
|
|
||
| There are 100 documents in ``randomSamples``. Running the sample code 5 | ||
| times produces the following output which approaches the expected value | ||
| of 50 matches in a collection this size. | ||
|
|
||
| .. code-block:: javascript | ||
| :copyable: false | ||
|
|
||
| { "numMatches" : 49 } | ||
| { "numMatches" : 52 } | ||
| { "numMatches" : 54 } | ||
| { "numMatches" : 48 } | ||
| { "numMatches" : 59 } | ||
|
|
||
| .. note:: | ||
|
|
||
| This example shows that the number of documents selected is | ||
| different each time. If you need to select an exact number of | ||
| documents, consider using :pipeline:`$sample` instead of ``$rand``. | ||
|
|
||
| .. seealso:: | ||
|
|
||
| :query:`$rand (query) <$rand>`, :pipeline:`$sample`, :expression:`$round` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| ============================= | ||
| Miscellaneous Query Operators | ||
| ============================= | ||
|
|
||
| .. default-domain:: mongodb | ||
|
|
||
| .. contents:: On this page | ||
| :local: | ||
| :backlinks: none | ||
| :depth: 1 | ||
| :class: singlecol | ||
|
|
||
| .. include:: /includes/extracts/operators-toc-explanation.rst | ||
|
|
||
| .. list-table:: | ||
| :widths: 25,75 | ||
| :header-rows: 1 | ||
|
|
||
| * - Name | ||
|
|
||
| - Description | ||
|
|
||
| * - :query:`$comment` | ||
|
|
||
| - Adds a comment to a query predicate. | ||
|
|
||
| * - :query:`$rand` | ||
|
|
||
| - Generates a random float between 0 and 1. | ||
|
|
||
| .. versionadded:: 4.4.2 | ||
|
|
||
|
|
||
| .. toctree:: | ||
| :titlesonly: | ||
| :hidden: | ||
|
|
||
| /reference/operator/query/comment | ||
| /reference/operator/query/rand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.