目录:
MongoDB文档的查找:
查询所有域:db.collectionName({条件},{域1:0},{域2:1})
- 0为不显示,1为显示,除了ID域其他域查找值必须相等
- 查找所有文件
示例1:db.acct.find().pretty() - 带条件查找
示例2:db.collectionName.find({条件1},{条件2})
3)查找一笔
示例:db.acct.findOne({acct_type:1}) - 指定查看ID域
示例:db.acct.find({},{_id:0}).pretty() - 指定查询ID域,acc_no,acc_na
示例:db.acct.find({},{_id:0,acc_no:1,acc_na:1})
MongoDB比较操作符:
MongoDB:
- 等于、不等于:$eq $ne
e.g. db.acct.find({acct_type:{ $eq:1}}) - 大于、小于: $gt $lt
e.g. db.acct.find({balance:{ $gt:5000 }}) - 大于等于、小于等于: $gte $lte
e.g.db.acct.find({balance:{ $gte:5000 }})
MongoDB逻辑操作符:
MongoDB:
$ and:和,$ or:或,$ not:否,$ nor:既不也不
- $ and 和
e.g. db.acct.find({$and:[{acct_type:1},{balance:{ $gt:5000}}]})
查询账户类型为1并且余额大于5000 - $ or 或
e.g. db.acct.find({ $or:[{acct_type:1},{acct_type:2}]})
查询账户类型为1或者为2的数据 - $ not 非
e.g. db.acct.find({balance:{ $not:{ $gt:5000} }})
查询余额小于等于5000 - $ nor 既不也不
e.g. db.acct.find({acct_na:{ $nor:[{acct_na:‘TOM’},{acct_na:‘Jerry’}]}})
查找既不是tom也不是jerry的账户
MongoDB数组的操作:
-
创建带有数组的集合Student,其中Score域包含多个值,为一个数组
e,g, db.Student.insert({name:‘Duke’,age:18,score:[60,70,80]})
e,g, db.Student.insert({name:‘Rokie’,age:22,score:[85,70,90,95]})
e,g, db.Student.insert({name:‘Cool’,age:26,score:[90,95,92]}) -
查询语句
- 查找学生成绩中包含70的数据
db.student.find({score:70}).pretty() - 查找学生成绩中同时包含70,90的数据
$ all 语句
db.Student.find({score:{$all:[70,90]}}).pretty() - 查找包含3门成绩的所有数据
$size 语句
db.Student.find({score:{ $size:3 }}).pretty()
- 查找学生成绩中包含70的数据
MongoDB其他操作:
- $ exists: 判断域是否存在
e.g. db.Student.insert({name:‘Loong’,age:22,score:[70,77,88,99],sex:1})
查找含有sex域的所有数据(true包含,false不包含)
db.Student.find({sex:{ $exists:true}}) - $ mod: 求余数
查找年龄为奇数的学生信息
e.g. db.Student.find({age:{ $mod:[2,1]}}) - $type : 根据域的类型查找(跟数据类型码)
e.g. 查找年龄为正数的所有数据
db.Student.find({age:{ $type:1}).pretty()
e.g. 查找所有名字为字符串的
db.Student.find({name:{ $type:2}}).pretty()
MongoDB查找函数:
- distinct: 去重,查看某个域的取值范围
e.g. 查看acct 及集合中acct_type一共多少种
db.acct.distinct(‘acct_type’) - limit:显示查找结果的前N笔
e.g. 查看acct中的前3笔数据
db.acct.find().limit(3) - skip: 跳过查找结果的前N笔数据
e.g. 跳过查找结果的前两笔
db.acct.find().skip(2) - sort: 对查询结果排序
语法格式:sort({域名称,1/-1})[1:升序,-1:降序]
e.g. 对所有账户按照余额降序排列
db.acct.find().sort({balance:-1})
e.g. 查找所有账户,按照账户类型、余额升序排序
db.acct.find().sort({acct_type:1,balance:1})
MongoDB文档删除:
- 格式:db.collectionName.remove(query,juestOne)
参数:query 筛选条件,不写则删除所有
justOne 布尔值,指定删除一笔[true:删除一笔,false:所有符合条件的数据]
e.g. 删除一笔类型为1的数据- db.acct.remove({acct_type:1},true)
MongoDB文档操作:
-
格式:db.collection.update(query,update,upsert,multi)
参数:- query:筛选条件
- update:修改操作符,表示修改的内容
- upsert:布尔值,指定在没有找到满足条件数据的时候,要不要执行插入true-插入,false-不插入
- multi:布尔值,指定是都修改多笔数据,true-修改所有满足条件的数据,false-只修改第一条
e.g. 修改某个账户余额($set)
- db.acct.update({acct_nu:value},{ $set:{balance:9999}},false,false)
-
删除域: $unset
e.g. 删除某个账户的reg_date域,使用unset将某个域设置为空
db.acct.update({key:value},{ $unset:{key:value}}) -
修改域: $rename
增加一个域,方便修改:- db.acct.update({},{$set:{revoke_date:Date()}},false,true)
- db.acct.update({},{$rename:{revoke_date:‘close_date’}},false,true)
-
加法修改器: $inc (做加法运算)
e.g. 为某个账户余额增加1000
- db.acct.update({“acct_nu” : “3227000002”},{$inc:{balance:1000}},false,false)
-
乘法修改器: $mul
e.g. 在某个账户上增加2%的利息- db.acct.update({“acct_nu” : “3227000002”},{$mul:{balance:0.02}},false,true)
-
取最小的值: $min [用指定域和某个值做对比]
e.g. 指定账户修改年龄- db.Student.update({“name” : “Loong”},{$min:{age:30}},false,false)
-
取最大的值: $max [用指定域和某个值做对比]
e.g. 指定账户修改年龄- db.Student.update({“name” : “Loong”},{$max:{age:15}},false,false)
-
向数组中添加一个值: $ push
e.g. 向Student集合中score添加一门成绩
db.Student.update({name:},{ $push:{score:90}},falase,false) -
向数据中添加多个值 $ pushAll
e.g. 向Student集合中score添加两门成绩
db.Student.update({name:‘Cool’},{ $pushAll:{score:[100,69]}},false,false) -
从数组中删除某个值 $ pull
e.g. 删除刚刚添加的数据
db.Student.update({name:‘Cool’},{ $pull:{score:90}},false,false) -
对应删除多个值 $pullAll
e.g. 删除刚添加的多个值
db.Student.update({name:‘Cool’},{ $pullAll:{score:[100,69]}},false,false) -
逐一取出其中的值进行操作 $each
e.g. 利用 $push , $each组合添加多个值
db.Student.update({name:‘Cool’},{ $push:{score:{ $each:[70,80,90]}}},false,false) -
向数组指定位置插入值 $ position
e.g. 插入成绩到第二位
db.Student.update({name:‘Cool’},{ $push:{score:{ $each:[110], $position:1}}},false,false) -
把数组排序重新写入 $sort
e.g. 排序score
db.Student.update({name:‘Cool’},{ KaTeX parse error: Expected '}', got 'EOF' at end of input: push:{score:{each:[],$sort:1}}},false,false) -
弹出数组中的一项 $pop
e.g. 把数组中第一项弹出 -1 ,最后一项 1
db.Student.update({name:‘Cool’},{ $pop:{score:-1}},false,false) -
向数组中添加一个数据,已经存在的不重复 $addToset
e.g. 添加已经存在的值
db.Student.update({name:‘Cool’},{ $addToSet:{score:120}},false,false)
MongoDB索引操作:
- 提高查询效率的技术避免全表扫描
- 索引并不是越多越好,数据少尽量不要建立索引
- 查看和创健删除索引
- db.Student.getIndexes() [查看索引]
- db.Student.createInsex({name:1}) [创建索引]
- db.Student.dropIndex(‘name_1’) [不带索引默认删除所有索引]
- 复合索引。根据多个与创建索引
e.g. 在acct_no,acct_na 创建索引
db.acct.createIndex({acct_no:1,acct_na:1}) - 唯一索引: 域的值不重复用unique指定唯一索引
db.acct.createIndex({acct_no:1},{unique:true})
- 域和索引的引号不能少
- 稀疏索引
- 稀疏索引:如果文档的域不存在,则不对该域创建索引
- sparse 参数设置为True
e.g. 在cust_info集合fax域创建稀疏索引
db.cust_info.createIndex({fax:1},{sparse:true})
- e.g. 查找score域死一项值为70的数据
db.Student.find({‘score.0’:70})
MongoDB日期时间操作:
MongoDB记录的是ISODate(格里尼治时间)
- Date() : 取当前时间,字符串
- new Date(): 取系统时间,对象
- ISODate() :取系统时间,对象
- 按照指定时间创建时间对象:
new Date(‘time’) - 将ISO时间转换为本地时间
ISODate().tolocaleSring
MongoDB内嵌文档:
- 文档的某个域还是一个文档,叫内嵌文档
- 内嵌文档引用格式:
- 外部文档.内部文档.域名称