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