NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,特别是大数据应用难题。
js吞噬作用:Any application that can be written in JavaScript, will eventually be written in JavaScript.。
这句话的出处是Jeff Atwood在2007年写的博客《The Principle of Least Power》,Jeff Atwood是Stack Overflow的联合创始人。
安装
1、 官网下载

下一步下一步傻瓜式操作即可
2、简单使用

双击执行mongod.exe
即可在你的电脑上启动一个mongodb服务

双击执行mongo.exe
即可启动一个客户端去链接操作Mongodb数据库
使用
1、建库
use xxxx
2、建表
db.xxx.insert({})
3、增
db.xxx.insert({})
for(var i = 0;i<2;i++){db.user.insert({"age":i})}
4、查
全表查询
db.xxx.find()
条件查询
db.xxx.find({"age":1})
5、删
db.xxx.remove({})//不给条件是全集合删除
6、改
db.xxx.update({},{})//第一个参数是条件对象 ,第二个参数是目标数据
修改mongodb的默认配置

image.png
在
bin文件夹中修改mongod.cfg文件即可
在egg中使用mongodb
1、链接
//安装egg-mongoose
npm install egg-mongoose -S
在plugin.js中开启
mongoose:{
enable: true,
package: 'egg-mongoose',
}
在config.default.js中配置
config.mongoose={
url: prod ? 'mongodb:eggadmin:123456@localhost:27017/bloguser' : 'mongodb://127.0.0.1:27017/bloguser',
options: {
useUnifiedTopology: true,
}
}
2、准备实体类
app/model
针对user表创建一个实体类
User.js
'use strict';
/**
* @description: Mongoose book Schema,
*/
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: { type: String }, /* 书籍名称 */
});
return mongoose.model('user', UserSchema);
};
在代码中使用
this.ctx.model.User.find({
});