node如何连接mongodb数据库

解决这两接口废弃的代码在下方,cv即可运行
(node:11064) 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.
(node:11064) DeprecationWarning: 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.

首先我们新建文件夹test
在当前文件夹下打开命令行窗口

依次输入以下代码

npm init

enter

npm install express mongodb --save

enter
不要以为我们的工作已经做完了

你还要开机啊

mongod --dbpath 数据库路径

enter

现在我们依赖的包已经装好了
接下来在我们新建的文件夹下面新建aap.js文件
复制以下代码运行
这里的代码是经过修改的,官网上的代码使用过程中会报两个接口即将废弃的错误
直接运行可以看到已经连接到数据库上了,并且不会报错

var express = require("express");
var app = express();
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017/haha';
MongoClient.connect(url, {useNewUrlParser:true ,useUnifiedTopology: true},function (err,db) {
    assert.equal(null,err);
    console.log("Connected correctly to Server");
    db.close();
})

app.get("/",function(req,res){
    res.send("Hello");
})
app.listen(3000);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装mongoose 在命令行中执行以下命令: ``` npm install mongoose ``` 2. 连接数据库Node.js中使用mongoose连接数据库需要先引入mongoose模块: ```javascript const mongoose = require('mongoose') ``` 接着使用mongoose.connect()方法连接数据库: ```javascript mongoose.connect('mongodb://localhost:27017/databaseName', { useNewUrlParser: true }) ``` 其中,mongodb://localhost:27017/databaseName是连接数据库的URL,其中localhost为数据库服务器地址,27017为MongoDB的默认端口号,databaseName为要连接数据库名称。 { useNewUrlParser: true }是Mongoose的一个选项,以允许在连接时使用新的URL字符串解析器。 3. 定义Schema和Model 在使用mongoose之前,需要先定义Schema和Model。Schema是用来定义数据结构的,Model是由Schema生成的实例。 ```javascript const Schema = mongoose.Schema const userSchema = new Schema({ username: String, password: String, email: String }) const User = mongoose.model('User', userSchema) ``` 以上代码定义了一个名为User的Model,该Model对应的Schema定义了三个属性:username、password和email。 4. CRUD操作 通过定义好的Model可以进行CRUD操作。以下是一些常用的操作: - 新增数据 ```javascript const user = new User({ username: 'Tom', password: '123456', email: 'tom@example.com' }) user.save(function (err, user) { if (err) return console.error(err) console.log(user.username + ' saved to database.') }) ``` - 查询数据 ```javascript User.find(function (err, users) { if (err) return console.error(err) console.log(users) }) ``` - 更新数据 ```javascript User.findOneAndUpdate({ username: 'Tom' }, { password: '654321' }, function (err, user) { if (err) return console.error(err) console.log(user) }) ``` - 删除数据 ```javascript User.deleteOne({ username: 'Tom' }, function (err) { if (err) return console.error(err) console.log('User deleted.') }) ``` 以上代码演示了如何使用mongoose连接MongoDB数据库,并进行CRUD操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值