[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:
Backports
Depends
is depended on by SERVER-48474 Add $sampleRate match expression Closed
is depended on by SERVER-48464 Remove {$rand:{const:true}} Closed
Documented
is documented by DOCS-13883 Investigate changes in SERVER-30405: ... Closed
Duplicate
is duplicated by SERVER-32928 $rand function Closed
Problem/Incident
Related
related to SERVER-43666 Sort by "randVal" is not random Closed
is related to SERVER-22123 Add an option to the $sample stage to... Backlog
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: SERVER-30405: add expression to generate a random number

SERVER-48612 Fix rand.js for aggregation_facet_unwind_passthrough

SERVER-48464 Remove {$rand:{const:true}}
Branch: v4.4
https://github.com/mongodb/mongo/commit/d9f68b03826fa545328b8611caa39d0bc90a5a6f

Comment by Githook User [ 11/Sep/20 ]

Author:

{'name': 'Svilen Mihaylov', 'email': 'svilen.mihaylov@mongodb.com', 'username': 'smihaylov-mongodb'}

Message: SERVER-30405: add expression to generate a random number
Branch: v4.4
https://github.com/10gen/mongo-enterprise-modules/commit/3278dcbaa1d07820d1066487ecfc58961421aa55

Comment by Svilen Mihaylov (Inactive) [ 02/Jun/20 ]

https://mongodbcr.appspot.com/621970001/

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.

db.foo.remove({a:true, {$expr:{$lt:["$$RANDOM", 0.001]}}})

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

{$and: [{$eq: [4, 6]}, {$gt: [{$add: [4, 6]}, 11]}]}

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.

Generated at Thu Feb 08 04:23:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.