Data insertion is throwing a MongoError: Unsupported OP_QUERY command: insert. The client driver may require an upgrade.
var writeDataToMongo = function (data) {
sendPostRequest(
`${process.env.API_URL}/db/insert`,
{ json: data },
(error, res, body) => {
if (!error && res.statusCode === 200) {
console.log(`sent data to store`);
} else {
console.log(`error sending data to store: ${error} ${body}`);
}
}
);
};
app.post('/db/insert', (request, response) => {
if (!request.body) {
return failure(response, '/db/insert needs post request body');
}
console.log(`got request to insert into ${request.body.colname}`);
const databaseName = request.body.dbname;
const collectionName = request.body.colname;
if (!collectionName) {
return failure(response, '/db/insert needs collection');
}
if (!databaseName) {
return failure(response, '/db/insert needs database');
}
const database = connection.db(databaseName);
// Add collection if it doesn't already exist
if (!database.collection(collectionName)) {
console.log('creating collection ' + collectionName);
database.createCollection(collectionName);
}
const collection = database.collection(collectionName);
const data = _.omit(request.body, ['colname', 'dbname']);
// log(`inserting data: ${JSON.stringify(data)}`);
collection.insert(data, (err, result) => {
if (err) {
return failure(response, `error inserting data: ${err}`);
} else {
return success(response, `successfully inserted data. result: ${JSON.stringify(result)}`);
}
});
});
Exact error:
error sending data to store: null [store] error inserting data: MongoError: Unsupported OP_QUERY command: insert. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal
I am not sure what is meant by upgrading “client driver”? I was using the latest version of MongoDB, Mongoose and node. After coming across some forums, I tried downgrading my MongoDB version to mongodb-community@4.4, mongoose to 5.0.1 and node to 20.11.0. It is still not working. Any ideas on what is going wrong?