MongoDB vs mysql
MongoDB | mysql | 名称 |
---|---|---|
document | record | 记录 |
collection | table | 表 |
database | database | 数据库 |
MongoDB
安装
window版本
- 选择一个磁盘创建数据库和日志的存储
例如:
E:\mongodb\data\db
E:\mongodb\data\log
- 创建配置文件 比如 c:\Program Files\MongoDB\Server\3.4\mongod.cfg
systemLog:
destination: file
path: E:\mongodb\data\log\mongod.log
storage:
dbPath: E:\mongodb\data\db
- 启动配置文件 (必须管理员身份)
> mongod --config "C:\Program Files\MongoDB\Server\3.4\mongod.cfg" --install
我将该目录已配置到全局变量中,若没有配置则进入mongoDB安装目录的bin目录再启动
-
启动MongoDB server
net start MongoDB
-
停止MongoDB server
net stop MongoDB
-
删除MongoDB server
“C:\ProgramFiles\MongoDB\Server\3.4\bin\mongod.exe” --remove
centos
安装
$ brew update
$ brew tap mongodb/brew
$ brew install mongodb-community
如何没有安装请戳右边链接👉Homebrew
或者将下面代码复制到终端运行
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
默认安装存储地址
非M1芯片 | M1芯片 | |
---|---|---|
configuration file | /usr/local/etc/mongod.conf | /opt/homebrew/etc/mongod.conf |
log directory | /usr/local/var/log/mongodb | /opt/homebrew/var/log/mongodb |
data directory | /usr/local/var/mongodb | /opt/homebrew/var/mongodb |
创建数据存储目录并赋予权限
$ sudo mkdir -p /data/db
$ sudo chown -R `id -un` /data/db
运行
$ mongod --datapath='指定data路径'
# 采用默认启动方式
$ brew services start mongodb-community
常用命令行
- 查看版本号
$ mongo --version
- 打开客户端
$ mongo
- 显示所有数据库
$ show dbs
- 创建数据库
# 使用blog 数据库
$ use blog
*: 一下操作都是在 blog 数据库中操作
- 显示所有集合
# 显示该库下的所有集合
$ show collections
- 创建集合
$ db.user.insert({name:'cc',age:20})
- 集合中查询数据
# 当不传入条件时则查询所有
$ db.user.find()
# 条件查询
$ db.user.find({name:'cc'})
- 删除数据
$ db.user.deleteOne({name:'cc'})
- 权限登录
$ db.auth('账号',‘密码’)
Mongoose在node中实践
是mongoDB的一个对象模型库,封装了mongoDB对文档的一些增删改查等常用方法,让nodejs操作mongoDB数据库变得更容易
Schema
是一种文件形式存储的数据库模型骨架,不具备数据库的操作能力,即定义数据类型
例如:
var PersonSchema=new mongoose.Schema({
name:String;
})
Model
由Schema构造生成的模型,具有抽象属性和行为的数据库操作
var PersonModel=db.model('person',PersonSchema)
entity
由Model创造的实体,他的操作也会影响数据库,可以操作数据库CRUD
var personEntity=new PersonModel({
name:'kk'
})
实践
连接
const mongoose = require('mongoose');
//1 连接数据库
const conn=mongoose.connect('mongodb://localhost/cms');
/**
* 有密码的连接方式
* 数据库名 test
* 用户名 admin
* 密码 123456
* */
// mongoose.connect('mongodb://admin:123456@localhost:27017/test');
//2. 定义数据类型
const userSchema = new mongoose.Schema({
name: String,
age: Number,
});
//3. 创建模型
const User = mongoose.model('User', userSchema);
简单 crud
//4. 查询user表中所有数据
User.find({}, (err, doc) => {
if (err) {
console.log(err);
return;
}
console.log(doc);
})
//5. 添加数据
var u = new User({
name: 'cc',
age: 20,
})
u.save((err) => {
if (err) {
console.log('添加失败');
return;
}
console.log('添加成功');
})
//6. 更新数据
User.updateOne(
{'_id': '5c4e9fc8b60c4b4e543bd1bc'},
{name: 'wcc'},
(error,doc)=>{
if(error){
return console.log('update error');
}
console.log(doc);
} )
//7. 删除数据
User.deleteOne({'_id':'5c4ea2289012181bec2d71ac'},(error,result)=>{
if(error){
console.log('delete error');
return;
}
console.log('删除成功',result);
})
补充
var userSchema = new mongoose.Schema({
name:{
type:String, //指定类型
trim:true, //去掉两边空格
},
sn:{
type:String,
index:true, //创建索引 或者使用 unique
},
age: Number,
redirect:{
type:String,
set(parmas){ //自定义修饰符,下面代码是对用户输入的网址进行判断,如何没有输入http 或者 https 则进行补全
if(!parmas) return '';
if(parmas.indexOf('http://')||parmas.indexOf('https://')){
//如果不是http或者https开头则走下面的逻辑
return 'http://'+parmas
}else{
return parmas;
}
}
}
});
数据校验
小结:
- require 表示该必须传入
- max与min 只能用在number类型中
- 枚举类型只能用在string类型中
var userSchema = mongoose.Schema({
name:{
type:String, //指定类型
require:true
},
sn:{
type:String,
index:true, //创建索引 或者使用 unique
maxlength:20, //只能用于string类型
minlength:10
},
age: {
type:Number, //min与max必须在Number中
min:0,
max:130
},
redirect:{
type:String,
},
status:{
type:String,
default:'1',
enum:['0','1'] //status 值必须在枚举值中并且枚举类型中type必须是String
}
});
数据库的导入和导出
//导入
// -h 主机(本机 localhost)
// -d 表示数据库名
// -o 表示目录
mongodump -h dbhost -d dbname -o dbdirectory
// mongodump -h localhost -d cms -o C:\Users\lanyee\Desktop
//导出
mongorestore -h dbhost -d dbname <path>