当前位置:网站首页>Mongodb在node中的使用
Mongodb在node中的使用
2022-07-06 09:29:00 【社会你磊哥,命硬不弯腰】
安装mongodb
首先先认识一下这个数据库,MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
安装并配置这个数据库参考官网即可!本篇博客主要说明在该数据库与node怎样建立连接并且实现增删改查等主要功能!
安装mongoose
我们在与数据库建立连接的过程中使用的是mongoose这个第三方包!
$ npm install mongoos
设置集合结构,字段名称是表结构中的属性名称,约束的目的是保证没有脏数据!
var Schema = mongoose.Schema;
var userSchame = new Schema({
username: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
mobelphone: {
type: Number
}
})
连接数据库,连接的是test数据库!
mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true });
将文档结构发布为模型,mongoose会将第一个大写字符串的作为数据库的名称,并将其小写字符串复数作为一个集合!
var User = mongoose.model('User', userSchame);
var admin = new User({
username: "gaochi",
age:18,
mobelphone: "12345678"
});
增
admin.save(function (err, ret) {
if (err) {
console.log("wrong!")
}
else {
console.log(ret);
}
})
删
User.remove(
{
name: 'renjialei' },
function (err, ret) {
if (err) {
console.log("删除失败");
} else {
console.log("删除成功");
}
})
改
User.updateMany({
username: 'renjialei' }, {
age: 17 }, function (err, ret) {
if (err) {
console.log("更改失败");
} else {
console.log("更改成功");
}
})
查
User.find(
//这块写的是我们的查询条件!
/* { username: 'gaochi' }, */
function (err, ret) {
if (err) {
console.log('err!');
} else {
console.log(ret);
}
})
最后附加上几个mongodb的简单命令
mongod //开启数据库!
mongo //开启数据库之后,该命令可以将数据库和客户端连接起来!
show dbs //展示数据库集合名称
use (collections)//进入数据库集合,没有则创建!
db.(collections).find() //查找集合中的数据!
show collections //展示数据库里面的所有集合!
db.(collections).insertOne({
}) //向数据库里面添加数据!
补充一个非常重要的知识点,mongod命令可以启动mongodb程序,必须在启动命令的那个盘的根目录下创建一个/data/db的文件夹,保存我们的数据,而不是在下载mongodb的那个目录下去创建文件夹!mongo命令则在我们的项目文件下去使用,将项目和数据库连接起来!
边栏推荐
- Codeforces Round #771 (Div. 2)
- ~73 other text styles
- LeetCode 1584. Minimum cost of connecting all points
- 腾讯面试算法题
- Shell_ 04_ Shell script
- LeetCode 1557. The minimum number of points that can reach all points
- Shell_ 05_ operator
- The concept of spark independent cluster worker and executor
- Continue and break jump out of multiple loops
- Cmake error: could not create named generator visual studio 16 2019 solution
猜你喜欢

Shell_ 07_ Functions and regular expressions

~87 animation

Shell_ 00_ First meeting shell
![Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]](/img/a1/7dd41e75d6768159317b65e436030d.jpg)
Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]

was unable to send heartbeat

字节跳动海外技术团队再夺冠:高清视频编码已获17项第一

7-5 blessing arrived

FLV格式详解

「博士毕业一年,我拿下 ACL Best Paper」

ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
随机推荐
Solve the single thread scheduling problem of intel12 generation core CPU (II)
LeetCode 1637. The widest vertical area between two points without any point
Usage of insert() in vector
Design of DS18B20 digital thermometer system
redux使用说明
Shell_ 00_ First meeting shell
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
Full record of ByteDance technology newcomer training: a guide to the new growth of school recruitment
Introduction to microservices
Redis standalone startup
原型链继承
@RequestMapping、@GetMapping
QT system learning series: 1.2 style sheet sub control lookup
SQL快速入门
Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
Shell_ 06_ Judgment and circulation
Detailed explanation of FLV format
js垃圾回收机制和内存泄漏
LeetCode 1020. Number of enclaves
LeetCode 1566. Repeat the pattern with length m at least k times