shell mongodb

1、启动mongodb
root@test:~#/home/test/mongodb/bin/mongod --dbpath=/usr/local/mongodb/mongodb_data/ --logpath=/usr/local/mongodb/mongodb_log/mongodb.log --logappend &
2、进入mongodb命令行
root@test:~# /home/test/mongodb/bin/mongo
MongoDB shell version: 1.8.1
connecting to: test
>
3、显示所有的数据库
> show dbs                                                                                                   
admin 0.0625GB
local (empty)
siyuroom 1.49951171875GB
>

4、使用数据库siyuroom
> use siyuroom
switched to db siyuroom
5、显示当前数据库
> db
siyuroom

6、显示库的所有集合
> show collections
siyuroom_log
system.indexes
userinfo
v9_picture_favorite
7、显示表的记录条数
> db.userinfo.find().count()
9

8、插入数据
> db.userinfo.insert({"id":1,"username":"test"})
> db.userinfo.insert({"id":2,"username":"victor"})
> db.userinfo.insert({"id":3,"username":"yansiyu"})
9、显示记录
> db.userinfo.find();
10、记录分页
> db.userinfo.find().skip(0).limit(2); //一页显示2条,第一页
> db.userinfo.find().skip(2).limit(2);//一页显示2条,第二页
11、查询username=test
> db.userinfo.find({"username":"test"})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }

12、查询等于
> db.userinfo.find({"age":29})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }

12、查询小于$lt
> db.userinfo.find({"age":{$lt:30}})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }

13、查询大于$gt
> db.userinfo.find({"age":{$gt:28}})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }
>

14、查询或$or
> db.userinfo.find({$or:[{"username":"test"},{"username":"user_1"}]})
{ "_id" : ObjectId("5285c7d339481305c608697b"), "username" : "user_1" }
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }

15、查询$in
> db.userinfo.find({"username":{$in:["victor","test",'user_1']}}) 
{ "_id" : ObjectId("5285c7d339481305c608697b"), "username" : "user_1" }
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }
>

16、查询$nin
> db.userinfo.find({"username":{$nin:["victor","test",'user_1']}})
{ "_id" : ObjectId("5285c7d339481305c608697c"), "username" : "user_2" }
{ "_id" : ObjectId("5285c7d339481305c608697d"), "username" : "user_3" }
{ "_id" : ObjectId("5285c7d339481305c608697e"), "username" : "user_4" }
{ "_id" : ObjectId("5285c7d339481305c608697f"), "username" : "user_5" }
{ "_id" : ObjectId("5285c7d339481305c6086980"), "username" : "user_6" }

17、查询正则
> db.userinfo.find({"username":/^yan/})                                                                      
{ "_id" : ObjectId("5285c85b39481305c6086985"), "username" : "test", "age" : 29 }

18、查询where
> db.userinfo.find({$where:function(){return this.username=='test'}})
19、更新update
> var info=db.userinfo.findOne({"username":"test"}) //一定要用findOne
> info.username="test115"
>  db.userinfo.update({"username":"test"},info)
20、局部更新$set
> db.userinfo.update({"username":"test115"},{$set:{"username":"test"}})
21、去重distinct
> db.userinfo.distinct("username")
[ "victor", "yansiyu", "test115" ]
22、查询时选择字段
> db.userinfo.find({},{"age":1,"_id":0}) //选择字段age,并且_id不显示,{}表示没有指定查询条件
{ "_id" : ObjectId("5285c7d339481305c6086982") }
{ "_id" : ObjectId("5285c7d339481305c6086983") }
{ "_id" : ObjectId("5285c81c39481305c6086984") }
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29 }
{ "_id" : ObjectId("5285d0fe50e2c9c9a6dbd401"), "age" : 30 }

 
> db.userinfo.find({"username":"victor"},{"age":1})        
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29 }
>

23、查询时过滤不包含查询字段的文档
> db.userinfo.find({"username":{$exists:true},"age":{$exists:true}})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29, "username" : "victor" }
{ "_id" : ObjectId("5285d0fe50e2c9c9a6dbd401"), "username" : "test", "age" : 30 }
>

> db.userinfo.find({"age":{$exists:true},"username":"victor"})
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29, "username" : "victor" }
>

24、查询时排序

> db.userinfo.find().sort({"username":-1})
{ "_id" : ObjectId("5285d0fe50e2c9c9a6dbd401"), "username" : "test", "age" : 30 }
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29, "username" : "victor" }
{ "_id" : ObjectId("5285c7d339481305c6086983"), "username" : "user_9" }
 

25、查询匹配所有$all
> db.userinfo.find({hobby:{$all:['zuqiu','qiuqiu']}})
{ "_id" : ObjectId("5285df288408c1e0c997c211"), "username" : "yansiyu", "age" : 100, "hobby" : [ "zuqiu", "qiuqiu" ] }
>

> db.userinfo.find({hobby:{$all:['qiuqiu','zuqiu']}})
{ "_id" : ObjectId("5285df288408c1e0c997c211"), "username" : "yansiyu", "age" : 100, "hobby" : [ "zuqiu", "qiuqiu" ] }
>
26、查义取模% mod
> db.userinfo.find({"age":{$mod:[2,0]}})  //查询age%2==0的字段
{ "_id" : ObjectId("5285d0fe50e2c9c9a6dbd401"), "username" : "test", "age" : 30 }
{ "_id" : ObjectId("5285ddd78408c1e0c997c210"), "username" : "yansiyu", "age" : 100, "hobby" : [ "pingpang", "lanqiu" ] }
{ "_id" : ObjectId("5285df288408c1e0c997c211"), "username" : "yansiyu", "age" : 100, "hobby" : [ "zuqiu", "qiuqiu" ] }
>

27、数组元素个数$size
> db.userinfo.find({"age":{$exists:true}})                                  
{ "_id" : ObjectId("5285c85b39481305c6086985"), "age" : 29, "username" : "victor" }
{ "_id" : ObjectId("5285d0fe50e2c9c9a6dbd401"), "username" : "test", "age" : 30 }
{ "_id" : ObjectId("5285ddd78408c1e0c997c210"), "username" : "yansiyu", "age" : 100, "hobby" : [ "pingpang", "lanqiu" ] }
{ "_id" : ObjectId("5285df288408c1e0c997c211"), "username" : "yansiyu", "age" : 100, "hobby" : [ "zuqiu", "qiuqiu" ] }
{ "_id" : ObjectId("5285e2648408c1e0c997c212"), "username" : "test115", "age" : 28, "hobby" : [ 1, 2, 3, 4, 5, 6 ] }
>

 
> db.userinfo.find({"age":{$exists:true},"hobby":{$size:2}})
{ "_id" : ObjectId("5285ddd78408c1e0c997c210"), "username" : "yansiyu", "age" : 100, "hobby" : [ "pingpang", "lanqiu" ] }
{ "_id" : ObjectId("5285df288408c1e0c997c211"), "username" : "yansiyu", "age" : 100, "hobby" : [ "zuqiu", "qiuqiu" ] }
>

 
> db.userinfo.find({"age":{$exists:true},"hobby":{$size:6}})
{ "_id" : ObjectId("5285e2648408c1e0c997c212"), "username" : "test115", "age" : 28, "hobby" : [ 1, 2, 3, 4, 5, 6 ] }
>

28、mongodb游标
> db.userinfo.find().forEach(function(u){printjson(u);})
或者
> for(var c=db.userinfo.find();c.hasNext();){
... printjson(c.next())
... }

再或者
> var cursor=db.userinfo.find();
> while(cursor.hasNext()){
... printjson(cursor.next());                                                                        
... }

再再或者
> db.userinfo.find().forEach(printjson)
> var info=db.userinfo.find()                   
> printjson(info[1])
{ "_id" : ObjectId("5285c7d339481305c608697c"), "username" : "user_2" }

> var info=db.userinfo.find().toArray()
> info[1]
{ "_id" : ObjectId("5285c7d339481305c608697c"), "username" : "user_2" }
>

    A+
发布日期:2013年12月08日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: