Appearance
增删改查分页
增加数据
javascript
this.ctx.model.Article.create(post, callback);
1
备注:其中 post 为 json 数据结构,callback 为操作后的回调函数
查询数据
获取所有数据,返回是一个数组
javascript
this.ctx.model.Article.find();
1
获取一个数据,返回是一个对象
javascript
this.ctx.model.Article.findOne();
1
条件查询
javascript
this.ctx.model.Article.find(conditions, callback);
1
condition 有以下几种类型
根据具体数据进行查询
javascript
this.ctx.model.Article.find({_id:5c4a819fb87ba4002a47bc4f,title:"123"},callback);
1
- 返回_id 为 5c4a819fb87ba4002a47bc4f,title 为 123 的结果
条件查询
"$lt" | 小于 |
---|---|
"$lte" | 小于等于 |
"$gt" | 大于 |
"$gte" | 大于等于 |
"$ne" | 不等于 |
javascript
this.ctx.model.Article.find({“sort”:{ $get:18 , $lte:30 });
1
- 返回 Article 表中 sort 大于等于 18 并小于等于 30 的结果
或查询 OR
"$in" | 一个键对应多个值 |
---|---|
"$nin" | 同上取反, 一个键不对应指定值 |
"$or" | 多个条件匹配, 可以嵌套 $in 使用 |
"$not" | 同上取反, 查询与特定模式不匹配的文档 |
javascript
this.ctx.model.Article.find({"title":{ $in:[20,21,22."haha"]} );
1
- 返回 Article 表中 title 等于 20 或 21 或 21 或"haha"的结果
javascript
this.ctx.model.Article.find({ $or: [{ age: 18 }, { name: "wxw" }] });
1
- 返回 Article 表中 age 等于 18 或 name 等于"wxw"的结果
类型查询("$exists"条件判定)
javascript
this.ctx.model.Article.find(
{ name: { $exists: true } },
function (error, docs) {
//返回Article表中所有存在name属性的结果
}
);
1
2
3
4
5
6
2
3
4
5
6
javascript
this.ctx.model.Article.find(
{ telephone: { $exists: false } },
function (error, docs) {
//返回Article表中所有不存在telephone属性的结果
}
);
1
2
3
4
5
6
2
3
4
5
6
匹配正则表达式查询
MongoDb 是使用 Prel 兼容的正则表达式库来匹配正则表达式
javascript
this.ctx.model.Article.find({ name: /joe/i });
1
- 返回 Article 表中 name 为 joe 的结果, 并忽略大小写
查询数组
javascript
this.ctx.model.Article.find({ array: 10 });
1
- 返回 Article 表中 array(数组类型)键中有 10 的文档, array : [1,2,3,4,5,10] 会匹配到
javascript
this.ctx.model.Article.find({ "array[5]": 10 });
1
- 返回 Article 表中 array(数组类型)键中下标 5 对应的值是 10, array : [1,2,3,4,5,10] 会匹配到
javascript
this.ctx.model.Article.find({ array: [5, 10] });
1
- 返回 Article 表中查询匹配 array 数组中既有 5 又有 10 的结果
javascript
this.ctx.model.Article.find({ array: { $size: 3 } });
1
- 返回 Article 表中查询匹配 array 数组长度为 3 的的结果
javascript
this.ctx.model.Article.find({ array: { $slice: 10 } });
1
- 返回 Article 表中查询匹配 array 数组的前 10 个元素
javascript
this.ctx.model.Article.find({ array: { $slice: [5, 10] } });
1
- 返回 Article 表中查询匹配 array 数组的第 5 个到第 10 个元素
7.3.7 where
用它可以执行任意 javacript 语句作为查询的一部分,如果回调函数返回 true 文档就作为结果的一部分返回
javascript
this.ctx.model.Article.find({ $where: "this.x + this.y === 10" });
this.ctx.model.Article.find({
$where: " function(){ return this.x + this.y ===10; } ",
});
1
2
3
4
2
3
4
- 其中 this 为数据表中的数据,上述返回 Article 表中属性 x+属性 y=10 的所有数据
8 删除数据
javascript
this.ctx.model.Article.remove(conditions, callback);
1
备注:conditions 为查询条件,与查询数据介绍的一样,eg:{ _id:5c4a819fb87ba4002a47bc4f },找到_id 为 5c4a819fb87ba4002a47bc4f 的数据,callback 为操作成功后的回调函数
9 更新数据
9.1 更新数据
javascript
this.ctx.model.Article.update(conditions, update, callback);
1
- 参数 1:查询条件, 参数 2:更新对象,可以使用 MondoDB 的更新修改器
备注:conditions 与查询数据中介绍的一样
9.2 update 为更新对象
javascript
let post = {
wid: "5c492c57acbe363fd4824446",
column: ["新闻"],
titleHead: "",
img: "",
isAbstract: "false",
};
this.ctx.model.Article.update({ _id: "5c4a819fb87ba4002a47bc4f " }, post);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 查询 Article 表中特定_id,并对 post 中所包含的属性进行更新。
update 使用 MondoDB 的更新修改器,有以下几种使用场景
9.2.1 "$inc"增减修改器,只对数字有效
javascript
this.ctx.model.Article.update({ age: 22 }, { $inc: { age: 1 } });
1
- 找到 age=22 的文档,修改文档的 age 值自增 1
9.2.2 '$set' 指定一个键的值,这个键不存在就创建它.可以是任何 MondoDB 支持的类型.
javascript
this.ctx.model.Article.update({ _id:5c4a819fb87ba4002a47bc4f }, { $set: { isDelete: true } });
1
- 对 5c4a819fb87ba4002a47bc4f 表进行软删除,找到特定_id 数据,增加或者修改 isDelete 属性
9.2.3 "$unset"同上取反,删除一个键
javascript
this.ctx.model.Article.update({ age: 22 }, { $unset: { age: 18 } });
1
- 执行后 age 键不存在
9.2.4 '$push'给一个键 push 一个数组成员,键不存在会创建,对数组有效
javascript
this.ctx.model.Article.update({ name: "wxw" }, { $push: { array: 10 } });
1
- 返回 Article 表中 name 为 wxw 的数据,增加一个 array 键,类型为数组,有一个成员 10
9.2.5 '$addToSet'向数组中添加一个元素,如果存在就不添加
javascript
this.ctx.model.Article.update({ name: "wxw" }, { $addToSet: { array: 10 } });
1
- 返回 Article 表中 name 为 wxw 的数据,array 中有 10 所以不会添加
9.2.6 '$each'遍历数组和 $push 修改器配合可以插入多个值
javascript
this.ctx.model.Article.update(
{ name: "wxw" },
{ $push: { array: { $each: [1, 2, 3, 4, 5] } } }
);
1
2
3
4
2
3
4
- 返回 Article 表中 name 为 wxw 的数据,执行后 array : [10,1,2,3,4,5]
9.2.7 '$pop' 向数组中尾部删除一个元素
javascript
this.ctx.model.Article.update({ name: "wxw" }, { $pop: { array: 1 } });
1
- 返回 Article 表中 name 为 wxw 的数据,其中 array : [10,1,2,3,4,5],执行后 array : [10,1,2,3,4]
- tip:将 1 改成-1 可以删除数组首部元素
9.2.8 '$pull' 向数组中删除指定元素
javascript
this.ctx.model.Article.update({ name: "wxw" }, { $pull: { array: 10 } });
1
- 返回 Article 表中 name 为 wxw 的数据,匹配到 array 中的 10 后将其删除。
10 排序(sort)
javascript
this.ctx.model.Article.sort({ isSetTop: -1, sort: 1, editTime: -1 });
1
- 对 Article 表中的数据进行排序,先按“isSetTop”降序,再按“sort”升序,最后按“editTime”降序
备注:键对应数据中的键名,值代表排序方向,1 升序, -1 降序。
11 限制返回结果的数量(limit)
javascript
this.ctx.model.Article.limit(3);
1
- 对 Article 表中的数据进行返回,返回为前面 3 条数据
12 跳过前 3 个文档,返回其余的(skip)
javascript
this.ctx.model.Article.skip(3);
1
- 对 Article 表中的数据进行返回,跳过前面 3 条数据,返回其余数据
附:综合使用最后三个方法进行分页查询
javascript
this.ctx.model.Article.find({ _id:5c4a819fb87ba4002a47bc4f }).skip(pageSize * (pageNum - 1)).limit(parseInt(pageSize)).sort({ isSetTop: -1, sort: 1, editTime: -1 });
1
- 其中 pageSize 和 pageNum 为动态传递数据,返回 Article 表中特定_id 在每页数据为 pageSize 条件下的第 pageNum 页中的数据,并按照“isSetTop”降序,再按“sort”升序,最后按“editTime”降序进行排序。