Registry / database / mongodb-ns

mongodb-ns

JSON →
library3.2.0jsnpmunverified

MongoDB namespace parsing and validation library that splits strings like 'db.collection' into components, checks validity, and identifies special namespaces (oplog, system, command). Current version 3.2.0, published under the @mongodb-js scope. Actively maintained as part of MongoDB DevTools Shared. Differentiators: strict validation of database and collection names per MongoDB rules, hash generation, and support for dotted collection names.

npm install mongodb-ns
INSTALL
IMPORT
SIG · MONGODB-NS
M
mongodb-ns
databasejavascriptv3.2.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.

default
import A from 'mongodb-ns'
const A = require('mongodb-ns').default
ESM default import; v3 is ESM-only, no default export for CJS require()
NS
import { NS } from 'mongodb-ns'
const { NS } = require('mongodb-ns').default
Named export; available in ESM only
isMongos
import { isMongos } from 'mongodb-ns'
const { isMongos } = require('mongodb-ns')
CJS require() works for named exports via require('mongodb-ns') but ESM import is preferred
type exports
import type { NS } from 'mongodb-ns'
TypeScript type imports; package ships types

Parses a MongoDB namespace string into its components, checks validity.

import parseNamespace from 'mongodb-ns'; const ns = parseNamespace('canadian-things.songs-aboot-bacon'); console.log(ns.database); console.log(ns.collection); console.log(ns.validDatabaseName); console.log(ns.toString());
Debug
Known issues
breakingVersion 3.0.0 removed CJS support; the package is now ESM-only.
fix
Use import syntax instead of require(). If you must use require(), use dynamic import() or upgrade to ESM.
affects: >=3.0.0
breakingDefault export changed from a function that returns an NS object to a function that returns a plain object in v2. In v3, the default export is the parseNamespace function (identical to named export).
fix
Use default import: import parseNamespace from 'mongodb-ns'.
affects: >=3.0.0
deprecatedThe 'NS' class export is deprecated in favor of using the parseNamespace function directly.
fix
Use parseNamespace('db.collection') instead of new NS('db.collection').
affects: >=2.0.0
gotchaThe namespace string must contain a dot; if not, the parser may return unexpected results (database = empty string, collection = full string).
fix
Always provide a string in 'database.collection' format. For system collections, use prefix 'system.'
affects: all
gotchaValidation of database names is strict: they cannot contain empty spaces, dots (except as separator), or special characters like '/', '\', '"', '$'. Collection names cannot start with 'system.' unless intentional.
fix
Ensure your namespace follows MongoDB naming rules; use ns.validDatabaseName and ns.validCollectionName to check.
affects: all
Errors
Common errors & fixes
TypeError: ns is not a function
Using CJS require() on ESM-only package v3+.
fix
Change to import ns from 'mongodb-ns' or use dynamic import()
Cannot find module 'mongodb-ns'
Package not installed or version mismatch (v3 released under @mongodb-js scope? No, it's mongodb-ns).
fix
Run npm install mongodb-ns and ensure Node.js >=12 for ESM support.
Property 'database' does not exist on type 'string'
Treating the result of parseNamespace as a string instead of an object.
fix
Use parseNamespace(ns) which returns an object, not the raw string.
Upgrade
Version history
3.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources