One idea is to store the used up samples in an array and do your random query with the addition of the not-in-array-elements condition $nin
. Below is sample code for demonstrating $nin
, which you can edit and play around with on my Saturn Fiddle.
// Welcome to SaturnAPI!
// Start collaborating with MongoDB fiddles and accomplish more.
// Start your code below these comments.
// Create a new collection
var Posts = new Mongo.Collection(null);
//Insert some data
Posts.insert({
number: 1,
author: "Saturn Sam",
message: "Hello!"
});
Posts.insert({
number: 2,
author: "Saturn Sam2",
message: "Hello!"
});
Posts.insert({
number: 3,
author: "Saturn Sam3",
message: "Hello!"
});
// Returns all records
// Posts.find({}).fetch()
// Returns all records with `number` not equal to `2` or `1`
Posts.find({number: {$nin: [2, 1]}}).fetch()