Node course saving a document doesn't work

When following the code in the “CRUD Operations Using Mongoose: saving a document” part:
if I follow the course code and mongoose version, it gives me error:

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 D:\Javascript\mongo-demo\node_modules\mongodb-core\lib\connection\pool.js:595:61
    at authenticateStragglers (D:\Javascript\mongo-demo\node_modules\mongodb-core\lib\connection\pool.js:513:16)
    at Connection.messageHandler (D:\Javascript\mongo-demo\node_modules\mongodb-core\lib\connection\pool.js:549:5)
    at emitMessageHandler (D:\Javascript\mongo-demo\node_modules\mongodb-core\lib\connection\connection.js:309:10)
    at Socket.<anonymous> (D:\Javascript\mongo-demo\node_modules\mongodb-core\lib\connection\connection.js:452:17)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234: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'
}

If I upgrade the driver to current, error message become

(node:6408) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:6408) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

So I changed the code to
mongoose.connect('mongodb://localhost:27017/playground', { useNewUrlParser: true, useUnifiedTopology: true })
Now it just doesn’t respond and eventually timeout.

What’s the problem here? Any help is appreciated!

My OS is Win10, vs code version 1.74.2, Node version 16.14.2

can you wrap that code in try {} catch() {} block.
Here is the code:

try {
    mongoose.connect('mongodb://localhost:27017/playground', { useNewUrlParser: true, useUnifiedTopology: true });
    console.log('db connection successful...');
}
catch (ex) {
    console.log('could not connect!', ex.message);
}

You can also use .then() .catch() approach:

mongoose.connect('mongodb://localhost:27017/playground', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('connection successful...')
.catch(ex => console.log('could not connect!', ex);

Let us know!

hi maverick, thank you for your reply. I used then and catch as the video demonstrated. Here is what I have in the code when using Mongoose 5.0.1

mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log('Connected...'))
    .catch(err => console.error('Cound not connect...', err))

Then changed it to

mongoose.connect('mongodb://localhost/playground', {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('Connected...'))
    .catch(err => console.error('Cound not connect...', err))

when I upgrade to 6.8.2.

The output I posted above came from the these code.

Oh wait!

use this string for db connection url:

mongodb://127.0.0.1:27017/playground

I am 90% sure it will work. :sweat_smile:

1 Like

I tried it yesterday according to another post I found here, but didn’t work. Somehow I tried it again today and it worked. Thank you!

For anyone with the same problem in the future, in Mongoose 6.8.2, this code works without any error and warning

mongoose.set('strictQuery', false)

mongoose.connect('mongodb://127.0.0.1:27017/playground')
    .then(() => console.log('Connected...'))
    .catch(err => console.error('Cound not connect...', err))

@notaniceusername - I got the same warning.

Following the link gets us to

This suggests to me that MongoDB v6.0.2 (in my case is the issue)
Downloaded from Download MongoDB Community Server | MongoDB v5.0.14 and installed

Next edit your Environment Variables > SYSTEM > PATH
change C:\Program Files\MongoDB\Server\6.0\bin
to C:\Program Files\MongoDB\Server\5.0\bin

Open Command Prompt and run ‘mongod’ and check/confirm the output for v5.0.14

Back in VSCode, kill and re-run ‘node .\index.js’. I got more from my ‘result’ than Mosh does, but a check into MongoDB Compass and it shows my Document.

Hope this helps you

1 Like

My best guess is that the “save()” function in mongoose v5.0.1 uses a deprecated way of sending an INSERT command to MongoDB.

If you’re getting too much info form your ‘getCourses()’ in Windows, try modifying from Mosh’s code to add something called ‘lean()’

const courses = await Course
.find({author:‘Dale’, isPublished:true})
.lean()
.limit(2)
.sort({name:1})
.select({name:1, tags:1});
console.log(‘Courses:’, courses );

1 Like