MongoDB Aggregation with $sample very slow
问题 There are many ways to select random document from a mongodb collection (as discussed in this answer). Comments point out that with mongodb version >= 3.2 then using $sample in the aggregation framework is preferred. However, on a collection with many small documents this seems to extremely slow. The following code uses mongoengine to simulate the issue and compare it to the "skip random" method: import timeit from random import randint import mongoengine as mdb mdb.connect("test-agg") class