关于阅读《MongoDB管理与开发精要》的一些记录
关于阅读《MongoDB管理与开发精要》的一些记录
一、查询
基本查询
条件查询
db.collection.find({"field": {$gt: value}}); // field > value
db.collection.find({"field": {$lt: value}}); // field < value
db.collection.find({"field": {$gte: value}}); // field >=value
db.collection.find({"field": {$lte: value}}); // field <= value
$all查询 和 $in 查询
db.collection.find({age: {$all: [6, 8]}}); // all查询返回必须包含6和8的值
db.collection.find({age: {$in: [6, 8]}}); // in查询返回包含6或者8的值
$exists – 存在
db.collection.find({age: {$exists: true}}); // 存在age字段 {$exists:false} 为不存在
db.collection.find({age: null}); // 注意:此查询返回{age:null} 和 age不存在字段
db.collection.find({age: {$in:[null], "$exists": true}}); // 注意:此查询只返回{age:null} ,去掉了age不存在字段
n
e
、
ne、
n e 、 in、$nin – 不等于、包含、不包含
db.collection.find({age: {$ne: 20}}); // age不等于20
db.collection.find({age: {$in: [20,30]}}); // age包含为20或者30
db.collection.find({age: {$nin: [20,30]}}); // age不包含为20或者30
$size – 数组中个数
db.collection.find({age: {$size: 2}}); // age数组元素个数为2的
count()
db.collection.find({age: {$size: 2}}).count(); //
db.collection.find().skip(10).limit(5).count(true); // 注:如果要返回限制之后的记录数量,要使用count(true)或者count(非0)
sort – 排序
db.collection.find().sort({age: 1}) // age升序 asc
db.collection.find().sort({age: -1}) // age降序 desc
distinct - 去重
db.collection.distinct("name") // 按照name字段去重并返回一个不重复的列表 eg: ["Tom", "Jerry"]
group – 分组
// select a, b, sum(c) csum from collection where active = 1 group by a, b
db.collection.group({
key: {a: true, b: true}, // 要分组的列 group a, b
cond: { active: 1}, // 分组条件 where active = 1
reduce: function(obj.prev) { // 分组计算方法 sum(c)
prev.csum += obj.c
},
initial: { csum: 0} // 分组计算的初始值,即csum从0开始累加c
});
内嵌文档查询
db.collection.find({"author.name": "Tom"}); // 数据格式:{"author": {"name": "Tom"}}
正则表达式匹配
db.collection.find({name: {$not: /^B*/}}); // 查询不匹配“name=B*”的记录
联合查询 (待续)
保存和更新
基本更新 update
db.collection.update(criteria, objNew, upsert, multi);
// criteria: 查询条件 objNew: 更新内容 upsert: 不存在是否插入, multi: 更新一条还是多条
数据更新操作符
$inc
db.collection.update({name: "Tom"}, {$inc: {age: 5}}); // 给name=Tom的人age字段加5
$ set、$unset – 设置和删除字段
db.collection.update({name: "Tom"}, {$set: {age: 5}}); // 给name=Tom的人age替换为5
db.collection.update({name: "Tom"}, {$unset: {age: 1}}); // 删除name=Tom的人的age字段
数组相关处理
db.collection.update({userid:1}, {$push: {name: "Jerry"}}); //数组中追加 push
// {"userid":1, "name":["Tom"]} -- > {"userid":1, "name":["Tom", "Jerry"]}
db.collection.update({userid:1}, {$pushAll: {name: ["Jerry", "Li"]}}); //数组中追加多值 pushAll
// {"userid":1, "name":["Tom"]} -- > {"userid":1, "name":["Tom", "Jerry", "Li"]}
db.collection.update({userid:1}, {$pop: {name: -1}}); // 数组中pop出第一个值
// {"userid":1, "name":["Tom", "Jerry", "Li"]} -- > {"userid":1, "name":["Jerry", "Li"]}
db.collection.update({userid:1}, {$pop: {name: 1}}); // 数组中pop出最后个值
// {"userid":1, "name":["Tom", "Jerry", "Li"]} -- > {"userid":1, "name":["Tom", "Jerry"]}
// $pull 数组删除一个值
db.collection.update({userid:1}, {$pull: {name: "Jerry"}});
// {"userid":1, "name":["Tom", "Jerry", "Li"]} -- > {"userid":1, "name":["Tom", "Li"]}
// $pullAll 数组删除多个值
db.collection.update({userid:1}, {$pullAll: {name: ["Tom", "Li"]}});
// {"userid":1, "name":["Tom", "Jerry", "Li"]} -- > {"userid":1, "name":["Jerry"]}
// $addToset 添加一个值到数组内,而且只有当这个值不在数组内才增加
db.collection.update({userid:1}, {$addToset: {name: "Tom"}});
// {"userid":1, "name":["Jerry", "Li"]} -- > {"userid":1, "name":["Jerry", "Li", "Tom"]}
$rename - 字段重命名
db.collection.update({userid:1}, {$rename: {"name": "alias"}});
// {"userid":1, "name":["Tom"]} -- > {"userid":1, "alias":["Tom"]}