I’ve got some documents that look like:
{
_id: 3,
key: 3,
stuff: "Some data"
}
Some documents also have a signUpDate
We can populate a collection for demo purposes like this:
for(i=1; i<=100000; i++){
if(i%3===0)
db.numbers.insert({_id:i, key:i, stuff:"Some data", signUpDate: new Date()});
else
db.numbers.insert({_id:i, key:i, stuff:"Some data"});
}
… so a third of the documents have a signUpDate
What I’m trying to do is create a map reduce function that takes all the documents, where signUpDate is not null, and insert them into a separate collection, ordered randomly
Is this possible?
Ok, here’s a solution that works:
Using mongoshell:
First, we populate our data:
So now, we have a third of our data with a signUpDate.
Now, a super-simple mapreduce:
Next, ensureIndex to speed up sorting:
Now, find the numbers (randomly sorted)