nodejs系列-使用nodejs链接MongoDB数据库问题总结

参考文档

问题1. 不支持preferredcms_db_name、cms_db_collection选项

throw new error_1.MongoParseError(`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`);
        ^

MongoParseError: options preferredcms_db_name, cms_db_collection are not supported
问题解决:
  • step1:删除连接字符串中preferredcms_db_name、cms_db_collection选项配置
  • step2:重新连接

问题2:MongoServerSelectionError: Server at serverName:27017 reports maximum wire version 5, but this version of the Node.js Driver requires at least 6 (MongoDB 3.6)

我当前代码中的版本
{
  "dependencies": {
    "mongodb": "^5.1.0"
  }
}

服务器的mongod版本

解决方案:

假如你和我一样,使用的是mongodb

const { MongoClient } = require('mongodb');
const url = 'mongodb://XXXXXXX';
const client = new MongoClient(url);
const dbName = 'AAAAA';
async function main() {
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('XXXXXX');
  const findResult = await collection.find({}).project({ _id: 'XXXXXXX' }).toArray()
  console.log('Found documents =>', findResult);
  // the following code examples can be pasted here...

  return 'done.';
}

main()
  .then(console.log('Connected.'))
  .catch(console.error)
  .finally(() => client.close());

  • 文档:mongodb
  • step1:去官网对照版本点击这里,直达官网链接
  • step2:更新本地代码,使用对照版本(经过对照,我应当使用的最大版本号为4.1)
  • step3:重新下载依赖,运行代码
  • step4:运行代码,成功连接~

假如你使用的是mongoose,解决步骤如下:
const mongoose  = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/ele-admin') 
.then(() => console.log('数据库连接成功'))
.catch(err => console.log('数据库连接失败', err));
  • step1:去官网对照版本点击这里,直达官网链接

  • step2:更新本地代码,使用对照版本(经过对照,我应当使用的最大版本号为5.0.0)

  • step3:重新下载依赖,运行代码

  • step4:运行代码,成功连接~

  • 今天就写到这里啦~

  • 小伙伴们,( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ我们明天再见啦~~

  • 大家要天天开心哦

欢迎大家指出文章需要改正之处~
学无止境,合作共赢

在这里插入图片描述

欢迎路过的小哥哥小姐姐们提出更好的意见哇~~
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Node.js可以通过MongoDB的官方驱动程序来连接MongoDB数据库。以下是连接MongoDB数据库的步骤: 1. 安装MongoDB驱动程序 使用npm安装MongoDB驱动程序: ``` npm install mongodb --save ``` 2. 连接MongoDB数据库 使用MongoDB驱动程序的MongoClient对象来连接MongoDB数据库: ``` const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017/mydb'; MongoClient.connect(url, function(err, db) { if (err) throw err; console.log("数据库已创建!"); db.close(); }); ``` 在上面的代码中,我们使用MongoClient对象的connect()方法来连接MongoDB数据库。connect()方法接受两个参数:MongoDB数据库的URL和一个回调函数。回调函数接受两个参数:错误对象和MongoDB数据库的实例。 3. 插入数据 使用MongoDB驱动程序的insertOne()方法来向MongoDB数据库中插入数据: ``` const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017/mydb'; MongoClient.connect(url, function(err, db) { if (err) throw err; console.log("数据库已创建!"); const myobj = { name: "菜鸟教程", url: "www.runoob" }; db.collection("site").insertOne(myobj, function(err, res) { if (err) throw err; console.log("文档插入成功"); db.close(); }); }); ``` 在上面的代码中,我们使用MongoDB驱动程序的collection()方法来获取MongoDB数据库中的集合。然后,我们使用insertOne()方法向集合中插入数据。 以上就是连接MongoDB数据库的基本步骤。 ### 回答2: 在使用Node.js连接MongoDB数据库之前,我们需要先安装MongoDB数据库Node.js环境。 接着,我们需要使用npm安装MongoDB官方的Node.js驱动程序——mongodb,这个驱动程序提供了对MongoDB的访问和操作。安装命令为: ``` npm install mongodb ``` 然后,在Node.js应用中引入mongodb模块: ```javascript var MongoClient = require('mongodb').MongoClient; ``` 接着,我们需要定义MongoDB的地址和数据库名称,并使用MongoDB的驱动程序进行连接: ```javascript var url = 'mongodb://localhost:27017/myproject'; MongoClient.connect(url, function(err, db) { console.log("Connected successfully to server"); // 进行操作 db.close(); }); ``` 在连接成功后,我们可以进行数据库的操作,例如插入、查询、更新和删除。以下是一些操作示例: 插入数据: ```javascript // 插入一条数据 db.collection('documents').insertOne({'name': 'John'}, function(err, r) { console.log('Inserted a document'); }); // 插入多条数据 db.collection('documents').insertMany([ {'name': 'John'}, {'name': 'Bob'} ], function(err, r) { console.log('Inserted documents'); }); ``` 查询数据: ```javascript // 查询所有数据 db.collection('documents').find({}).toArray(function(err, docs) { console.log('Found the following records'); console.log(docs); }); // 查询满足条件的数据 db.collection('documents').find({'name': 'John'}).toArray(function(err, docs) { console.log('Found the following records'); console.log(docs); }); ``` 更新数据: ```javascript // 更新一条数据 db.collection('documents').updateOne({'name': 'John'}, {$set: {'age': 20}}, function(err, r) { console.log('Updated a document'); }); // 更新多条数据 db.collection('documents').updateMany({'name': 'John'}, {$set: {'age': 20}}, function(err, r) { console.log('Updated documents'); }); ``` 删除数据: ```javascript // 删除一条数据 db.collection('documents').deleteOne({'name': 'John'}, function(err, r) { console.log('Deleted a document'); }); // 删除多条数据 db.collection('documents').deleteMany({'name': 'John'}, function(err, r) { console.log('Deleted documents'); }); ``` 最后,在对数据库的操作完成后,我们需要关闭数据库的连接: ```javascript db.close(); ``` 通过以上步骤,我们可以在Node.js中连接MongoDB数据库,并进行数据库的操作。 ### 回答3: Nodejs是一个非常流行的后端开发语言,它可以通过一些模块来连接MongoDB数据库。 首先,需要安装NodejsMongoDB,可以通过官网下载并安装。MongoDB在安装好之后,需要启动MongoDB服务。 接下来,需要安装mongodb模块。可以在终端输入以下命令: ``` npm install mongodb --save ``` 这样就成功安装了MongoDB模块。 接着,需要在Nodejs中导入mongodb模块,并编写连接代码。以下是一个基本的连接代码: ```javascript const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017/test'; MongoClient.connect(url, function(err, db) { if (err) throw err; console.log("Database connected!"); db.close(); }); ``` 这个连接代码首先加载了`mongodb`模块,并设置了一个`url`常量,表示连接的数据库地址。然后使用`MongoClient`对象的`connect`方法连接数据库,如果成功连接则会输出“Database connected!”并关闭数据库连接。 在连接MongoDB数据库的时候,可以增加一些可选项。例如,可以指定需要认证的用户名和密码,或者更改默认的端口号等。 一旦连接成功,就可以对MongoDB数据库进行操作了。可以查看官方文档,了解更多的MongoDB操作方法。 需要注意的是,在使用MongoDB之后,需要关闭数据库连接以节省系统资源。可以使用`db.close()`方法来关闭数据库连接。 总之,使用Nodejs连接MongoDB数据库非常方便。只需安装一些必要的模块,编写一些简单的代码,就可以连接并操作MongoDB数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值