mongodb-collection-sample

JSON →
library 5.0.0 ·javascript
verified Jun 5, 2026

Samples documents from MongoDB collections using native $sample aggregation (MongoDB ≥3.1.6) or client-side reservoir sampling for older versions. Version 5.0.0 is stable and maintains backward compatibility. Key differentiator: automatic fallback between server-side and client-side sampling based on MongoDB version and sample size relative to collection count, avoiding blocking sorts. Supports options like query filter, sample size, projection, raw BSON, sort, maxTimeMS, and value promotion.