Random Sampling from Mongo
问题 I have a mongo collection with documents. There is one field in every document which is 0 OR 1. I need to random sample 1000 records from the database and count the number of documents who have that field as 1. I need to do this sampling 1000 times. How do i do it ? 回答1: Here's an example in the mongo shell .. assuming a collection of collname , and a value of interest in thefield : var total = db.collname.count(); var count = 0; var numSamples = 1000; for (i = 0; i < numSamples; i++) { var