Registry / database / mongodb-schema

mongodb-schema

JSON →
library12.7.0jsnpmunverified

Infer a probabilistic schema for a MongoDB collection by sampling documents. Version 12.7.0. It returns a detailed schema including field names, types, probabilities, duplicate detection, and encountered values. Works as a CLI tool or programmatically via Node.js. Unlike schema validation tools, it analyzes existing data to infer structure. It is actively maintained by MongoDB and supports semantic type discovery.

npm install mongodb-schema
INSTALL
IMPORT
SIG · MONGODB-SCHEMA
M
mongodb-schema
databasejavascriptv12.7.0
harness data pending
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.
fix
Use 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.
fix
Update 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.
fix
Use --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.
fix
Use 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.
fix
Use 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.
fix
Call 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.
fix
Wrap parseSchema call in a try-catch or use .catch() on the promise.
Upgrade
Version history
12.7.0latest on npm
Audit
Dependencies
mongodbrequiredMongoDB Node.js driver is required to connect to MongoDB and read documents when used programmatically.
Agent activity
2 hits · last 30 days
node
2
Resources
mongodb-schema — npm install mongodb-schema · libregistry