Brief introduction
MongoDB is a product with function between relational databases and non-relational databases (nosql), non-relational database among the most feature-rich, like most relational databases.
Mongoose
Mongoose library can be used to connect to the database in the Node
mongoose Chinese Documents
npm i mongoose -s
Basic Operation
Connect to the database
const mongoose = require('mongoose');
//连接指定数据库
mongoose.connect(
'mongodb://ip地址/数据库名称',
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
const conn = mongoose.connection;
//绑定连接完成监听
conn.on('connected',function () {
console.log('数据库连接成功');
})
Create a model
Schema used to define the data structure of the object
Model defined by mongoose.model (table name, data object)
//得到对应特定集合的model (文档:数据对象, 集合:数据表)
const Schema = mongoose.Schema;
const userSchema = new Schema({ //文档结构: 属性名\属性值
name: { type: String, default: 'hahaha' },
age: { type: Number, min: 18, index: true },
bio: { type: String, match: /[a-z]/ },
password: {type:String}
});
//定义Model
const UserModel = mongoose.model('user',userSchema);//创建了users集合
Additions and deletions to change search
Created by the Model step to implement the additions and deletions to change search
CRUD-related functions: https://mongoosejs.com/docs/queries.html
//增
function saveTest() {
//创建实例
const userModel = new UserModel({
username: 'admin',
age: 19,
bio:"z",
password:md5('12345') //利用 blueimp-md5库,将密码加密
});
userModel.save(function (err, data) {
console.log('save()',err,data);
})
}
//删
function deleteTest() {
UserModel.deleteOne(
{
_id: '5e4d1c324abf031e98915a65'
},(err, product) => {
console.log('delete ',err,product);
}
)
}
//查 通过find()/findOne()/findById
function findTest() {
UserModel.find(
(err,doc)=>{
console.log('find()',err,doc)
}
);
UserModel.findOne({username: 'admin'},
function (err,doc) {
console.log('findOne()',err,doc)
})
}
//改
function updateTest() {
UserModel.findByIdAndUpdate({
_id: '5e4d1c324abf031e98915a65'
},{
username: 'newName'
},
function (err,doc) {
console.log('findByIdAndUpdate',err,doc)
})
}
The official criteria query example
// With a JSON doc
Person.
find({
occupation: /host/,
'name.last': 'Ghost',
age: { $gt: 17, $lt: 66 },
likes: { $in: ['vaporizing', 'talking'] }
}).
limit(10).
sort({ occupation: -1 }).
select({ name: 1, occupation: 1 }).
exec(callback);
// Using query builder
Person.
find({ occupation: /host/ }). //模糊查询:正则表达式
where('name.last').equals('Ghost').
where('age').gt(17).lt(66).
where('likes').in(['vaporizing', 'talking']).
limit(10).
sort('-occupation').
select('name occupation').
exec(callback);
Conditional operator:
$ Within last Greater gt -------->$gte --------- gt equal >=
$lt -------- less than <
$lte --------- lt equal <=
$ne ----------- not equal !=
$eq -------- equal =
$ In: [] satisfying one of the array in which the opposite is $ Nin
limit read skip a specified number of records to skip a specified number of records
Ascending lifting keyword: 'asc' / 'ascending' / 1 descending Keywords: 'desc' / 'Descending' / -1
// to "field" Ascending and "test" Descending
query.sort ({field: 'asc ', Test: -1});
// equivalent to
query.sort (' field -test ');select for include and exclude certain attributes
// the include A and B, the exclude OTHER Fields
query.select ( 'A B');
// the exclude C and D, the include OTHER Fields
query.select ( '- C -d') ;