安装MongoDB和启动服务器
1.下载mongoDB服务: (3.1.2版本)
2.安装–>选择custom–>都下一步安装即可
3.在本地磁盘新建一个空目录,位置不要太深,比如:C:\db,这个目录是用来存放数据文件的
4.运行mongoDB
服务:进入mongoDB
安装路径下的bin
目录里,比如:C:\Program Files\MongoDB\Server\3.1\bin
,
打开命令行工具,git bash
、cmd
或者powershell
哪个能用用哪个
5.执行mongod --dbpath c://db
,只要没有出现报错,说明服务启动成功,默认端口号就是27017,这个窗口不要关闭,因为服务已启动!!!
6.可视化工具:robo3t 或者 navicat
关系型数据库与非关系型数据库的区别
关系型数据库 | 非关系型数据库 |
---|---|
MySql | MongoDB |
二维表 | json |
表结构相对固定的 | 数据结构更加灵活 |
database | database |
table | collection |
row(行) | document |
col(列) | filed(字段) |
sql语句 | js api |
MongoDB的使用
1.新建一个项目 ,初始化项目
npm init -y // 如果已经新建了项目,就省略该步骤
2.安装mongoose包
npm install mongoose // 或者 yarn add mongoose
3.在项目的app.js
里引入mongoose
模块,连接数据库
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/1912', {useNewUrlParser: true});
4.安装http-errors
包
npm install http-errors // 或者 yarn add http-errors
5.在localhost
运行的测试数据库有待处理的连接,如果连接成功或者发生连接错误,就执行以下代码
app.js
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("we're connected!")
});
存
6.如果连接上数据库,就可以操作数据库了,要是用mongoose数据库,就要以Schema开始,Schema作用是把非关系型数据库变为关系型的结构,就可以正确的指定collection里需要哪些字段
var UserSchema = new mongoose.Schema({
name: String,
age: Number,
password: String
});
7.基于Schema得到一个model
,把UserSchema
与users
关联
var User = mongoose.model('users', UserSchema);
8.基于schema
和model
,得到一个实例tom
var tom = new User({ name: 'tom', age: 100, password: "123" });
1-6步:是连接数据库;
7步:是定义一个结构UserSchema;
8步:是把这个结构跟users这个collection(类似于MySQL的表)关联;
9步:是根据结构得到一个document(类似于MySQL的一条记录),此时这个document还没有存到数据库里。
10.把document(tom)
存到数据库里
tom.save((err, tom) => {
if (err) return console.error(err);
else console.log(tom)
});
11.数据库里看看有没有存成功
取
12.查询数据(document)
User.find({ name: /^tom/ });
13.查询所有数据
User.find((err, docs) => {
if (err) return console.error(err)
else {
console.log(docs)
}
})
14.有条件的查询
User.find({
name:'zhangsan'
},(err, docs) => {
if (err) return console.error(err)
else {
console.log(docs)
}
})
15.大于、小于、正则
User.find({
age: { $gte: 19 }, //$gte:>= $lte:<= $gt:> $lt:<
password: /\d/
}, (err, docs) => {
if (err) return console.error(err)
else {
console.log(docs)
}
})
16.第二个参数:不写相当于select *
,写表示只查询这个字段
User.find({ password: /\d/ },'_id', (err, docs) => {
if (err) return console.error(err)
else {
console.log(docs)
}
})
17.skip跳过 limit限制
User.find({ password: /\d/ }, '_id', { skip: 1, limit: 1 }, (err, docs) => {
if (err) return console.error(err)
else {
console.log(docs)
}
})
删除
18.deleteOne()
User.deleteOne({ name: 'zhangsan' }, (err) => {
if (err) return console.error(err)
else {
console.log("deleted")
}
})
修改
19.{name:'tom'}
是条件
User.updateOne({ name: 'tom' }, { age: 80 }, (err) => {
if (err) return console.error(err)
else {
console.log("updated")
}
})
第二个参数
User.updateOne({ name: 'tom' }, { age: 80 }, (err, obj) => {
if (err) return console.error(err)
else {
console.log(obj)
}
})
{n:1,nModified:1,ok:1}
obj.n: ---- 条件能够查到的条数
obj.nModified: ---- 根据这个条件修改了多少条
obj.ok: ---- 1代表修改成功,0代表修改失败(err是网络连接失败等)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/4566.html