时间:2021-07-01 10:21:17 帮助过:2人阅读
$set: {name: "zhangsandege", sex: "female"},
$push: {phoneNum: "186xxx"},
$sort: {age: 1}
}, false, true);
- 查
- 查询
collections.find({name: "zhangsan"});
- 游标(数组的引用)
var cursor = studentsColl.find();
- 显示数据
var cursor = studentsColl.find();
cursor.forEach(function(obj) {
printjson(obj);
});
- 限制字段
studentsColl.find(query, {name: 1, age: 0})
- 1 表示显示该属性
- 0 表示去掉该属性
- 统计
cursor.count();
- 排序
cursor.sort({name: 1, age: -1});
- 1 升序
- -1 降序
- 正则
{name: {$regex: /^zhang.{0,}$/}}
- 分页
cursor.limit(num);
- num为限制数量
cursor.skip(num);
- num为跨过数量
cursor.skip(0).limit(10);
cursor.skip(10).limit(10);
- 去重
collection.distinct("name", {age: 18});
- 分组
var results = collections.group(
{key: {name: 1, age: 0}},
cond: {},
initial: {count: 0, vowels: 0, consonants: 0},
reduce: function(obj, prev) {
prev.count++;
prev.vowels += obj.stats.vowels;
prev.consonants += obj.stats.consonants;
},
finalize: function(obj) {
obj.total = obj.vowels + obj.consonants;
}
);
- 聚合
collections.aggregate({
{$match: {sex: "male"}},
{$group: {_id: "$className", largest: {$max: "$age"}, smallest: {$min: "$size"}},
{$sort: {_id: 1}}}
});
8.权限
MongoDB
标签: