非关系型数据库
优点:
-
- 高可扩展性
-
- 分布式计算
-
- 低成本
-
- 架构的灵活性,半结构化数据
-
- 没有复杂的关系
缺点:
-
- 没有标准化
-
- 有限的查询功能(到目前为止)
-
- 最终一致是不直观的程序
安装 mongoose
npm install mongoose
引入及连接
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
mongoose 带有connection对象用来保存连接状态,可以监控其连接状态:
const db = mongoose.connection;
db.on('error', () => {
console.error('连接错误!')
});
db.once('open', () => {
console.log('数据库已连接!');
});
创建Schema
const {Schema} = require('mongoose');
const newsSchema = mongoose.Schema({
title: String,
content: String,
})
以下是 mongoose 的所有合法 SchemaTypes:
-
String
-
Number
-
Date
-
Buffer
-
Boolean
-
Mixed
-
ObjectId
-
Array
-
Decimal128
Schema设置
const userSchema = new mongoose.Schema({
username: String,
password: String,
}, {
timestamps: true,
versionKey: false
})
versionKey:false 去掉自动生成_v字段(不使用版本控制)。
timestamps:true 由mongoose自动管理文档的创建和更新时间。
const userSchema1 = new mongoose.Schema({
username: String,
password: String,
}, {
timestamps: {
createdAt:'tm',
updatedAt:'update'
},
versionKey: false
})
数据格式的定义:
{
username: String,
username: {
type:String,
type:[],
type:[String],
//设置默认值
default:'123',
//当前项是否必填
required:true,
//字符串
//是否将字符串暖转小写
lowercase:true
//是否将字符串转大写
uppercase:true
//是否对字符串两端去空格
trim:true
//数字或日期类型
max:数字/日期
min:数字/日期
},
}
创建Model
const userModel = mongoose.model("users", userSchema,'abc');
新增
userModel.create({
title: "",
content: "",
},function(err,result){
})
const obj = new userModel({
title: "",
content: "",
});
obj.save(function(err,result){
})
查询:
userModel.findOne(
{
username:'张三'
},
function(err,result){
}
)
findOne找不到result是null,找到了就是一个对象(document)。
userModel.find({
username:'张三'
},function(err,result){})
findById()
查询条件:
userModel.find(
{
age:{
$gte:20,
$lte:30
}
},
function(err,result){
}
)
$gte 大于等于。
$lte 小于等于
$gt 大于
$lt 小于
$ne 不等于
取记录总数:
userModel.count(callback);
分页:
userModel.find().skip().limit().then(function(result){});
userModel.find().skip().limit().exec(function(err,result){});
排序
userModel.find().sort({username:1}).skip().limit().exec(function(err,result){});
//关键语句
sort({username:1})
对username进行排序,1表示升序,-1表示降序
移除
deleteOne({ name: 'Eddard Stark' }, function (err) {});
deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {});
findOneAndRemove({ name: 'Eddard Stark' }, function (err) {})
findByIdAndRemove(id,function (err) {})
更新
findByIdAndUpdate(id, update, callback);
updateOne({ name: 'Eddard Stark' }, update, callback)
Populate(填充)
//建立用户基本信息模型
const userSchema = new Schema({
username: String,
password: String,
userInfo: {
type: Schema.Types.ObjectId,
ref: 'userInfo'
}
});
//用户扩展信息模型
const userInfoSchema = new Schema({
name: String,
gender: String,
})
// Compile model from schema
const userModel = model('users', userSchema);
const userInfoModel = model('userInfo', userInfoSchema);
让用户表和用户信息表关联,需要在用户表中设置一个字段userInfo
:
userInfo: {
type: Schema.Types.ObjectId,
ref: 'userInfo'
}
ref与
const userInfoModel = model('userInfo', userInfoSchema);
userInfo关联。
用户表的userInfo字段,存储用户信息表的相关记录的_id的值。
在查询时:
userModel.findOne({
_id: '6308695b57d10770e47b36b2'
})
.populate('userInfo')
.then(function (result) {
res.json(result)
}
)
使用populate('userInfo')来填充字段。
userModel.find().populate('userInfo').populate('userextends').then()
then(function(result)) .exec(function(err,result))