Reputation: 43
I stuck up with this error for the last two days. I am new to node and MongoDB and because of this error, I could not proceed further.
Here is my code for reference.
const express = require('express');
//const app = express();
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/playground')
.then(() => console.log('connected to mongoDB'))
.catch(err => console.log('could not connect'))
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: { type: Date, default: Date.now },
isPublished: Boolean
});
const Course = mongoose.model('course', courseSchema);
async function createCourse(){
const course = new Course({
name: 'Angular Course',
author: 'Node Learner',
tags: [ 'Angular', 'backend'],
isPublished: true
});
const result = await course.save();
console.log(result);
}
createCourse();
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'
I browsed about this error and it always tells me to the client driver may require an upgrade.
My node.js version is the current stable version - 18.12.1.
My npm version is 8.19.2
I also checked my network connection and tried VPN too.
Upvotes: 4
Views: 23808
Reputation: 1
This is caused due to the usage of outdated version of packages. Use the following commands to resolve this error
Download latest version of node
nvm install 18
nvm use 18
Upgrade mongoose to latest version
npm i mongoose@latest
Get the latest version of mongodb here
Note: Make sure to update your path in environmental variables to match the bin folder of newly downloaded MongoDB version
Upvotes: -1
Reputation: 21
If your using Windows:
3.6.23
[email protected]
mongod
in command prompt, and if it doesn't start, try again after restarting windows.waiting for connections
, you are good to goThis is my exprience and how I solved it. Hope it helps
Upvotes: 0
Reputation: 1
I think your node is upgraded so try to use 127.0.0.1 instead of localhost in the connection string
Upvotes: 0
Reputation: 11
To run the provided code successfully, please ensure that you have MongoDB version 4.4 installed and the Mongoose library version 5.0.1. These specific versions are recommended for compatibility with the code snippet you shared.
Upvotes: 1
Reputation: 3186
In my case the problem was an old version of the connect-mongodb-session
npm package. Check if you use it.
I fixed the error by upgrading connect-mongodb-session
version 2.0.6 to the latest 3.1.1.
I have MongoDB 6.0.6 installed.
Upvotes: 1
Reputation: 9
The error is showing because of two reasons either mongoose version has become old or it has not been installed yet . try installing mongoose again
npm i mongoose
Upvotes: 0