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.
npm install mongodb-collection-sampleNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Connects to MongoDB, samples 5 documents from a collection using a readable stream.
Use event listeners ('data', 'end', 'error') or pipe to a writable stream.Review the package's fallback logic for performance; consider using native $sample if collection is large.
Ensure query is an object, e.g., { status: 'active' }, or omit it.Use `const sample = require('mongodb-collection-sample');` instead of `import sample from ...`Call `const db = client.db('databaseName');` and pass `db`.Run `npm install lodash` alongside this package.