MongoError:不支持的 OP_QUERY 命令:插入

问题描述 投票:0回答:2

我是 MongoDB 新手,我正在尝试使用 Nodejs 应用程序连接 mongodb。 每当我尝试运行 nodejs 文件时,就会引发错误。 MongoError:不支持的 OP_QUERY 命令:插入。客户端驱动程序可能需要升级。有关更多详细信息,请参阅https://dochub.mongodb.org/core/legacy-opcode-removal

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

const url = 'mongodb://localhost:27017';
const dbName = 'conFusion';

const client = new MongoClient(url);

client.connect()
.then((client) => {
    console.log('Connected correctly to server');
    const db = client.db(dbName);
    const collection = db.collection('dishes');
    collection.insertOne({name: "Uthapizza", description: "test"})
    .then((result) => {
        console.log('Insert Document:\n', result.ops);
        return collection.find({}).toArray();
    })
    .then((docs) => {
        console.log('Found Documents:\n', docs);
        return collection.deleteMany({});
    })
    .then((result) => {
        console.log('Deleted Documents:\n', result);
        return client.close();
    })
    .catch((err) => console.log(err));
})

我一直在使用mongoDB最新版本6.0,但我无法使用我的节点应用程序执行插入操作。

Connected correctly to server
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
    at C:\Users\koushik\OneDrive\Desktop\Nodejs\node-mongo\node_modules\mongodb-core\lib\connection\pool.js:593:63
    at authenticateStragglers (C:\Users\koushik\OneDrive\Desktop\Nodejs\node-mongo\node_modules\mongodb-core\lib\connection\pool.js:516:16)
    at Connection.messageHandler (C:\Users\koushik\OneDrive\Desktop\Nodejs\node-mongo\node_modules\mongodb-core\lib\connection\pool.js:552:5)
    at emitMessageHandler (C:\Users\koushik\OneDrive\Desktop\Nodejs\node-mongo\node_modules\mongodb-core\lib\connection\connection.js:309:10)
    at Socket.<anonymous> (C:\Users\koushik\OneDrive\Desktop\Nodejs\node-mongo\node_modules\mongodb-core\lib\connection\connection.js:452:17)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  errmsg: 'Unsupported OP_QUERY command: insert. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal',
  code: 352,
  codeName: 'UnsupportedOpQueryCommand'
}

请帮助我解决这个问题并请提及解决方案。我应该做什么来执行该操作,因为它不起作用并且 op_query 在最新版本中已被弃用。

node.js mongodb server backend
2个回答
3
投票

错误说明了您需要做什么(即使用最新的 mongo 节点驱动程序)。最新服务器停止支持旧版 OP_INSERT/OP_QUERY 有线协议


0
投票

检查您的

package.json
文件。 mongodb 版本应至少为 3.6,Mongoose 版本应为 >=5.0。我的
package.json
中的一个简单的 mongodb 版本更新到 ^3.6 就帮我完成了。

© www.soinside.com 2019 - 2024. All rights reserved.