0

How can I select N random records that satisfy a condition (i.e., key:value condition) in Pymongo. Something like

my_random_sample = collection.find({'notified'False},{'$samples':100})
amaatouq
  • 2,297
  • 5
  • 29
  • 50
  • In addition to the marked dupe, you can use the `$match` pipeline as your query i.e. `db.collection.aggregate([ { '$match': { 'notified': False } },{ '$sample': { '$size': 100 } } ])` – chridam Mar 01 '17 at 17:58
  • @chridam thanks .. but for that exact command, I get OperationFailure: unrecognized option to $sample: $size – amaatouq Mar 01 '17 at 19:25
  • Sorry, there was a typo in the code, that should be `db.collection.aggregate([ { '$match': { 'notified': False } },{ '$sample': { 'size': 100 } } ])` – chridam Mar 01 '17 at 19:33

0 Answers0