MongoError: Unsupported OP_QUERY command: insert. The client driver may require an upgrade

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?

Hey @Harshini_Chandrika,

Starting in MongoDB 6.0 the legacy opcodes were removed from the server so if you’re using a version of the Node.js driver that predates MongoDB 3.6 support (when the new OP_MSG opcode was added) you may still be using legacy opcodes without knowing it.

Based on the compatibility matrix MongoDB 3.6 support was added with driver v3.0, so you are likely using a very old version of Mongoose that still uses an old driver.

Per Mongoose’s server version compatibility chart it appears you just need to be using a version of Mongoose >= 5.0. Since you’ve indicated you’re using Mongoose 5.0.1 it may be worth just double checking your package.json to make sure the mongodb version being used is at least 3.0 (https://github.com/Automattic/mongoose/blob/5.0.1/package.json#L26 shows that it should be but it’s worth verifying).

If you need to stay on Mongoose 5.x, consider updating this to at least 5.2.0.

1 Like

Hi Alex,

I can’t believe that it was a simple mongodb version update in my package.json to ^3.6 that did it for me! Thank you!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.