Error
Error Code: 73

MongoDB Error 73: Invalid Database or Collection Name

📦 MongoDB
📋

Description

Error 73, 'Invalid Namespace', indicates that MongoDB encountered an issue with the name or structure of a database or collection reference. This typically occurs when an operation attempts to access, create, or modify a namespace (database.collection) that is malformed, contains invalid characters, or exceeds naming conventions.
💬

Error Message

Invalid Namespace
🔍

Known Causes

4 known causes
⚠️
Invalid Characters in Namespace
Database or collection names contain characters not allowed by MongoDB's naming rules, such as '$', '.', or spaces.
⚠️
Namespace Length Exceeded
The combined length of the database and collection name (database.collection) exceeds MongoDB's maximum length limit for namespaces.
⚠️
Using Reserved Names
Attempting to create or access a collection or database using a reserved name, such as 'admin.$cmd' or 'local.oplog.rs' in an incorrect context.
⚠️
Malformed Namespace String
The provided namespace string is incorrectly formatted, missing a database or collection component, or has an extra '.' character.
🛠️

Solutions

3 solutions available

1. Check Command Options easy

Verify options are valid for the operation

1
Check documentation for valid options
// Common invalid options:

// find() doesn't take 'limit' as option (use .limit())
db.users.find({}, { limit: 10 })  // Wrong
db.users.find({}).limit(10)  // Right

// Wrong option name
db.users.createIndex({ name: 1 }, { uniq: true })  // Wrong
db.users.createIndex({ name: 1 }, { unique: true })  // Right

2. Fix Index Creation Options medium

Use valid index options

1
Valid index options
db.users.createIndex(
  { email: 1 },
  {
    unique: true,        // Enforce uniqueness
    sparse: true,        // Only index documents with field
    expireAfterSeconds: 3600,  // TTL index
    background: true,    // Deprecated in 4.2+
    name: "idx_email"    // Custom name
  }
);

3. Fix Aggregation Options medium

Use valid aggregation pipeline options

1
Valid aggregation options
db.users.aggregate(
  [{ $match: { status: "active" } }],
  {
    maxTimeMS: 60000,
    allowDiskUse: true,
    cursor: { batchSize: 100 }
  }
);
🔗

Related Errors

5 related errors