当前位置:网站首页>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命令则在我们的项目文件下去使用,将项目和数据库连接起来!
边栏推荐
- Shell_ 03_ environment variable
- ~75 background
- SQL快速入门
- 搭建flutter环境入坑集合
- Tencent interview algorithm question
- 我走过最迷的路,是字节跳动程序员的脑回路
- Spark independent cluster dynamic online and offline worker node
- Error: case label `15 'not within a switch statement
- Chapter III principles of MapReduce framework
- Eureka single machine construction
猜你喜欢

I'm "fixing movies" in ByteDance

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

Redis standalone startup

Business system compatible database oracle/postgresql (opengauss) /mysql Trivia

Some instructions on whether to call destructor when QT window closes and application stops

我在字节跳动「修电影」

字节跳动技术面试官现身说法:我最想pick什么样的候选人

字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了

SQL quick start

~69 other ways to use icon fonts
随机推荐
LeetCode 1640. Can I connect to form an array
LeetCode 1552. Magnetic force between two balls
@RequestMapping、@GetMapping
FLV格式详解
字节跳动技术新人培训全记录:校招萌新成长指南
Simple records of business system migration from Oracle to opengauss database
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
After the subscript is used to assign a value to the string type, the cout output variable is empty.
~83 form introduction
LeetCode 1551. Minimum operand to make all elements in the array equal
Educational Codeforces Round 122 (Rated for Div. 2)
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
Chapter 6 datanode
~85 transition
Spark independent cluster dynamic online and offline worker node
Educational Codeforces Round 122 (Rated for Div. 2)
Codeforces Round #771 (Div. 2)
Record the error reason: terminate called after throwing an instance
QT system learning series: 1.2 style sheet sub control lookup
Cartesian tree (modified)