# Node course saving a document doesn't work

**URL:** <https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367>\
**Category:** Node\
**Created:** [December 31, 2022, 11:38pm UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367 "2022-12-31T23:38:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![notaniceusername](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@notaniceusername](https://forum.codewithmosh.com/u/notaniceusername)\
**Post date:** [December 31, 2022, 11:38pm UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/1 "2022-12-31T23:38:24Z")

</div>

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:

```auto
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

```auto
(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

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [January 1, 2023, 4:19am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/2 "2023-01-01T04:19:03Z")

</div>

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

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![notaniceusername](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@notaniceusername](https://forum.codewithmosh.com/u/notaniceusername)\
**Post date:** [January 1, 2023, 5:35am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/3 "2023-01-01T05:35:49Z")

</div>

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

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

```

Then changed it to

```auto
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.

---

<div class="post-metadata">

**Author:** ![maverick](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@maverick](https://forum.codewithmosh.com/u/maverick)\
**Post date:** [January 1, 2023, 9:39am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/4 "2023-01-01T09:39:57Z")

</div>

Oh wait!

use this string for db connection url:

`mongodb://127.0.0.1:27017/playground`

I am 90% sure it will work. 😅

---

<div class="post-metadata">

**Author:** ![notaniceusername](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@notaniceusername](https://forum.codewithmosh.com/u/notaniceusername)\
**Post date:** [January 1, 2023, 4:27pm UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/5 "2023-01-01T16:27:13Z")

</div>

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

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![djb160](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/djb160/32/4881_2.png) [@djb160](https://forum.codewithmosh.com/u/djb160)\
**Post date:** [February 17, 2023, 3:29am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/6 "2023-02-17T03:29:14Z")

</div>

@notaniceusername - I got the same warning.

Following the link gets us to

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/4/476b4438649cb4a1fa7d11cb759bc1628937e8cd.png)

This suggests to me that MongoDB v6.0.2 (in my case is the issue)  
Downloaded from [Download MongoDB Community Server | MongoDB](https://www.mongodb.com/try/download/community) 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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/9/9dc08f00e10a1a79b93f92fd1f8831c13c0effcc.png)

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

---

<div class="post-metadata">

**Author:** ![djb160](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/djb160/32/4881_2.png) [@djb160](https://forum.codewithmosh.com/u/djb160)\
**Post date:** [February 17, 2023, 3:32am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/7 "2023-02-17T03:32:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![djb160](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/djb160/32/4881_2.png) [@djb160](https://forum.codewithmosh.com/u/djb160)\
**Post date:** [February 17, 2023, 3:59am UTC](https://forum.codewithmosh.com/t/node-course-saving-a-document-doesnt-work/17367/8 "2023-02-17T03:59:26Z")

</div>

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 );
