Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
parseSchema
✓ import { parseSchema } from 'mongodb-schema'
✗ const parseSchema = require('mongodb-schema')
mongodb-schema ships ESM; use named import. CommonJS require will not work directly.
default
✓ import mongodbSchema from 'mongodb-schema'
✗ const mongodbSchema = require('mongodb-schema').default
If using default import, it works as an object with parseSchema, etc. CommonJS fallback is available via require('mongodb-schema').default.
parseSchema
✓ const { parseSchema } = require('mongodb-schema')
Older Node.js versions (CJS) can use destructured require. Note that this works due to ESM-CJS interop.
Parses a MongoDB collection and logs the probabilistic schema as JSON.
const { parseSchema } = require('mongodb-schema');
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);
async function main() {
await client.connect();
const db = client.db('test');
const cursor = db.collection('data').find();
const schema = await parseSchema(cursor);
console.log(JSON.stringify(schema, null, 2));
await client.close();
}
main().catch(console.dir);
Debug
Known issues
gotchaparseSchema expects a stream, cursor, or array of documents, not a collection handle.fixUse collection.find() or other readable stream as first argument.
affects: *
breakingVersion 5.0.0 removed node-mongodb-native 2.x support and changed the API to be promise-based.fixUpdate code to use async/await or promises; refer to changelog for migration.
affects: >=5.0.0
deprecatedThe CLI flag --no-values is deprecated and may be removed in future versions.fixUse --values=false or omit to include values.
affects: >=10.0.0 <12.0.0
gotchaWhen using CJS require, you must use require('mongodb-schema').default for default import.fixUse destructured require (const { parseSchema } = require('mongodb-schema')) for named exports. affects: >=12.0.0
Errors
Common errors & fixes
TypeError: parseSchema is not a function
Incorrect import: using require('mongodb-schema') without destructuring or .default.
fixUse const { parseSchema } = require('mongodb-schema'); or import { parseSchema } from 'mongodb-schema'. Error: Expected a stream, cursor, or array of documents
Passed a collection or database object instead of a readable stream.
fixCall collection.find() to get a cursor and pass that to parseSchema.
TypeError: Cannot read property 'count' of undefined
The result is undefined because parseSchema threw but it was not caught.
fixWrap parseSchema call in a try-catch or use .catch() on the promise.
Audit
Dependencies
mongodbrequiredMongoDB Node.js driver is required to connect to MongoDB and read documents when used programmatically.