Error
Error Code:
22
MongoDB Error 22: Corrupt or Malformed BSON Data
Description
MongoDB Error 22, 'Invalid BSON', indicates that the database received data that does not conform to the Binary JSON (BSON) specification. This error typically occurs during data insertion, update, or query operations when the BSON structure is malformed, incomplete, or corrupted.
Error Message
Invalid B S O N
Known Causes
4 known causesMalformed BSON Structure
Data sent to MongoDB does not adhere to the BSON specification, such as incorrect field types, missing delimiters, or improperly nested documents.
Data Corruption During Transmission
BSON data may become corrupted while being transmitted over the network or during file I/O operations before reaching the MongoDB server.
Incorrect Driver Serialization
The MongoDB driver used by the application might incorrectly serialize application-level data into BSON format, leading to invalid structures.
Manual BSON Construction Errors
If BSON is being manually constructed or manipulated, errors in encoding, byte order, or length calculations can result in invalid data.
Solutions
3 solutions available1. Identify and Remove Corrupt Documents medium
Locate and delete documents that are causing BSON parsing errors.
1
Connect to your MongoDB instance using the `mongosh` shell.
mongosh
2
Switch to the database containing the problematic collection.
use your_database_name
3
Attempt to query the collection. MongoDB will often indicate which document is causing the issue or the query might fail outright.
db.your_collection_name.findOne()
4
If a specific document's `_id` is identified (e.g., from logs or a failed query), use `remove` to delete it. This is a quick fix for a single corrupt document.
db.your_collection_name.remove({ _id: ObjectId('corrupt_document_id') })
5
If the corrupt document is not easily identifiable, you may need to iterate through your collection and attempt to read each document. This can be done with a script that logs any document causing an error.
db.your_collection_name.find().forEach(function(doc) {
try {
// Attempt to access a field to trigger potential BSON errors
var value = doc.someField;
} catch (e) {
print('Error processing document _id: ' + doc._id + ' - ' + e);
// Optionally, auto-remove the problematic document:
// db.your_collection_name.remove({ _id: doc._id })
}
});
6
After identifying and removing corrupt documents, restart your MongoDB service to ensure all caches are cleared.
sudo systemctl restart mongod
2. Rebuild the Collection from a Backup medium
Restore the collection from a known good backup, effectively overwriting corrupt data.
1
Ensure you have a recent, verified backup of your MongoDB data.
text
2
Stop the MongoDB service to prevent data inconsistencies during the restore process.
sudo systemctl stop mongod
3
Locate your backup files for the specific database or collection.
text
4
Use `mongorestore` to restore the data. If you have a full database backup, you can restore the entire database. If you only have a collection backup, specify the collection.
mongorestore --db your_database_name --collection your_collection_name /path/to/your/backup/file
5
Alternatively, if you have a dump from `mongodump`, you can restore from that.
mongorestore --db your_database_name /path/to/your/dump/directory
6
Start the MongoDB service.
sudo systemctl start mongod
3. Repair the MongoDB Data Files (Last Resort) advanced
Use MongoDB's repair utility to attempt to fix inconsistencies in the data files. This is a destructive operation and should be used with caution.
1
Stop the MongoDB service.
sudo systemctl stop mongod
2
Locate your MongoDB data directory (e.g., `/var/lib/mongodb`). You can find this in your `mongod.conf` file.
text
3
Execute the `mongod --repair` command, pointing to your data directory. This process can take a significant amount of time and disk space.
mongod --dbpath /var/lib/mongodb --repair
4
Once the repair process is complete, start the MongoDB service.
sudo systemctl start mongod
5
After the repair, it is highly recommended to perform a `mongodump` of your data and then restore it to a new data directory to ensure data integrity.
mongodump --out /path/to/new/dump
systemctl stop mongod
mv /var/lib/mongodb /var/lib/mongodb_old
mkdir /var/lib/mongodb
chown mongodb:mongodb /var/lib/mongodb
systemctl start mongod
mongorestore /path/to/new/dump