[SERVER-30405] add expression to generate a random number Created: 28/Jul/17 Updated: 30/Oct/23 Resolved: 04/Jun/20 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | 4.7.0, 4.4.2 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Asya Kamsky | Assignee: | Svilen Mihaylov (Inactive) |
| Resolution: | Fixed | Votes: | 2 |
| Labels: | BIC, asya, expression, needs-syntax | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||||||||||||||||||||||||||||||||||
| Backport Requested: |
v4.4
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Sprint: | Query 2020-06-01, Query 2020-06-15 | ||||||||||||||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||||||||||||||
| Linked BF Score: | 46 | ||||||||||||||||||||||||||||||||||||||||||||||||
| Description |
|
For generating a random number during aggregation or something like SERVER-22225 it would be nice to have a way to generate a (pseudo-)random number, either as an expression or an a system variable. |
| Comments |
| Comment by John Page [ 23/Sep/20 ] | |
|
Very timely - I actually implemented a PRNG in aggregation yesterday using basic operators - it's not very good so this will be useful. | |
| Comment by Githook User [ 11/Sep/20 ] | |
|
Author: {'name': 'Svilen Mihaylov', 'email': 'svilen.mihaylov@mongodb.com', 'username': 'smihaylov-mongodb'}Message:
| |
| Comment by Githook User [ 11/Sep/20 ] | |
|
Author: {'name': 'Svilen Mihaylov', 'email': 'svilen.mihaylov@mongodb.com', 'username': 'smihaylov-mongodb'}Message: | |
| Comment by Svilen Mihaylov (Inactive) [ 02/Jun/20 ] | |
| Comment by John Page [ 24/Oct/17 ] | |
|
I need this for in-pipeline monte-carlo and error minimisation functions. | |
| Comment by Asya Kamsky [ 24/Oct/17 ] | |
|
Another use case for random expression - deleting a random subset of documents.
If "$$RANDOM" generated a random value between 0 and 1, this would delete 1/1000th of documents where a is true. | |
| Comment by Charlie Swanson [ 01/Aug/17 ] | |
|
Today each expression has the property that repeated invocations will return the same thing. This allows us to do certain optimizations, such as replacing
with just the literal false. If an expression might return something different on the next evaluation, this kind of optimization gets more difficult. It's just a nice property of the language we have right now, and if we don't actually need to break it, I'd rather not. | |
| Comment by Asya Kamsky [ 31/Jul/17 ] | |
|
Isn't the whole point of a random value to return different results on repeated calls? Calling $sample will (hopefully) return different results on repeated calls, so I'm not sure I understand the hesitation... Is it about testing challenges or usability? | |
| Comment by Charlie Swanson [ 31/Jul/17 ] | |
|
asya I think SERVER-22123 originally requested an expression like this, but we hesitated to provide a function that wouldn't return the same results on repeated calls - this would make the behavior depend on our optimization techniques, which is a little weird, and we found the use case could be addressed by the proposal for an extra argument to $sample. |