Title

1.打开mongo.exe

2.创建db_test数据库: use db_test

3.向tb_test表中插入数据:db.tb_test.insert({name:"小强",age:23})

4.显示所有的表:show collections 或者 show tables

5.显示db_test数据库中的tb_test表:use db_test   db.tb_test.find()

6.显示db_test数据库中的所有表:db.getCollectionNames()

插入数据:

db.tb_test2.insert({name:"小强1",age:12,color:"yellow"})

db.tb_test2.insert({name:"小强2",age:12,color:"yellow"})

db.tb_test2.insert({name:"小强3",age:12,color:"yellow"})

7.查询表tb_test2中age为12的数据:db.tb_test2.find({age:12}) (显示三条)

8.查询表tb_test2中age为12的一条数据:db.tb_test2.find({age:12}) (显示最上面一条)

9.查询表tb_test2中age为12的数据,并且指定显示哪些字段:db.tb_test2.find({age:12},{name:1}) (三条记录,只显示_id,name两个字段)(条件为0不显示其他都显示,非0仅显示指定)

10.查询age在23到33之间的数据:db.tb_test2.find({age:{$lt:33,$gt:23}})  ($lt<,$gt>,$lte<=,$gte>=)

11.$in:db.tb_test2.find({age:{$in:["23","33"]}})  查询年龄在 23到33之间

12.$or:db.tb_test2.find({$or:[{name:"小强"},age:{$in:["23","33"]}}]) 查询年龄在23到33或者name为小强的数据

13.$not:同上

14.null:匹配null或者不存在的字段,$exists:匹配字段存在

db.tb_test2.insert({name:"小强"})

db.tb_test2.find({age:{$in:[null],$exists:true}}) 匹配age值为null和age字段不存在的数据中age字段存在的数据

15.查询包含数组的数据

db.tb_test2.insert({name:"小强",color:["yellow","black","red"]})

db.tb_test2.insert({name:"打野",color:["yellow","blue","red"]})

db.tb_test2.find(color:["yellow","blue","red"])  完全匹配(数量,顺序,内容)

db.tb_test2.find({color:{$all:["yellow","red"]}})  匹配同时包含yellow和red的数据(数据无关)

db.tb_test2.find({color:"white"}) 匹配数组中含有white的所有数据

16.排序:db.tb_test2.find().sort({age:1,name:-1}); 1升序,-1降序

17.分页:db.tb_test2.find().sort({age:1}).limit(3).skip(4); 取三条数据,从index为4的位置取

18.获取数量:db.tb_test2.find({name:null}).count();或者 db.tb_test2.count({name:null})

19.删除数据:db.tb_test2.remove({name:null})

20.修改数据:db.tb_test2.update({name:null},{age:23}); 修改 所有name为null的数据 age为23 其它字段都没有了。

db.tb_test2.update({name:null},{$set:{age:23}}); 修改name为null的记录的age属性值为23,其它字段不改变

21.增加field:db.tb_test2.update({name:null},{$inc:{height:175}});

22.

参考:http://www.cnblogs.com/eggTwo/p/4040580.html

posted on 2017-07-15 19:21  闫世超  阅读(2645)  评论(0编辑  收藏  举报
Title