MongoDB学习笔记【2】-- 试用

大部分内容根据MongoDB官方手册整理:http://docs.mongodb.org/manual/contents/

查看数据库

[root@slayer ~]# mongo
MongoDB shell version: 2.2.3
connecting to: test
> show dbs
local   (empty)
test    0.0625GB
>

使用use切换数据库,use一个新名字可以使用新数据库,MongoDB会等到插入数据才会建立

> use mydb
switched to db mydb
> show dbs
local   (empty)
test    0.0625GB
> db.mydb.insert({name:"slayer"})
> show dbs
local   (empty)
mydb    0.0625GB
test    0.0625GB
>

 

find看内容

> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
>

换个方式插入。。

> a = {1 : "x"}
{ "1" : "x" }
> b = {2 : "y"}
{ "2" : "y" }
> db.mydb.insert(a, b)
> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
> db.mydb.insert(b)
> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
{ "_id" : ObjectId("50c951d096c0253cce4a8c89"), "2" : "y" }

可以看到insert不支持多个参数

用循环插入

> for (i=0; i<7; ++i) db.mydb.insert({value: i})
> db.mydb.find()
{ "_id" : ObjectId("50c950ab96c0253cce4a8c87"), "name" : "slayer" }
{ "_id" : ObjectId("50c951ba96c0253cce4a8c88"), "1" : "x" }
{ "_id" : ObjectId("50c951d096c0253cce4a8c89"), "2" : "y" }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8a"), "value" : 0 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8b"), "value" : 1 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8c"), "value" : 2 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8d"), "value" : 3 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8e"), "value" : 4 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c8f"), "value" : 5 }
{ "_id" : ObjectId("50c9661596c0253cce4a8c90"), "value" : 6 }
>

继续插入

> db.mydb.xx.insert({1:3})
> db.mydb.xx.find()
{ "_id" : ObjectId("50c966b596c0253cce4a8c91"), "1" : 3 }

查看当前数据库 db 删除数据库 db.dropDatabase()

> db
mydb
> show dbs
local   (empty)
mydb    0.0625GB
test    0.0625GB
> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
> show dbs
local   (empty)
test    0.0625GB
> db

 

使用游标和循环来打印内容

> var c = db.mydb.find()
> while (c.hasNext()) printjson(c.next())
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c93"), "value" : 1 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c94"), "value" : 2 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c95"), "value" : 3 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c96"), "value" : 4 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c97"), "value" : 5 }
{ "_id" : ObjectId("50c96b7196c0253cce4a8c98"), "value" : 6 }
>

三个方法的官方说明:The hasNext() function returns true if the cursor has documents. The next() method returns the next document. The printjson() method renders the document in a JSON-like format.

hasNext() 返回游标是否指向文件,next 返回下一个文件。printjson 把文件用用类json格式打印出来。

使用游标的下标

> var c = db.mydb.find()
> printjson(c[0])
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
> printjson(c[4])
{ "_id" : ObjectId("50c96b7196c0253cce4a8c96"), "value" : 4 }

 

打印复杂点的json数据

> db.mydb.xx.insert({a : 1, b: {A : "x", B : {alpha : 0}}})
> var c = db.mydb.xx.find()
> printjson(c.next())
{
        "_id" : ObjectId("50c96eee96c0253cce4a8c99"),
        "a" : 1,
        "b" : {
                "A" : "x",
                "B" : {
                        "alpha" : 0
                }
        }
}
>

 

查找元素

> db.mydb.find({value:0})
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
> db.mydb.insert({value:0, xx:"a"})
> db.mydb.find({value:0})
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
{ "_id" : ObjectId("50c9707396c0253cce4a8c9a"), "value" : 0, "xx" : "a" }
> db.mydb.find({value:0}).limit(1)
{ "_id" : ObjectId("50c96b7196c0253cce4a8c92"), "value" : 0 }
>

 

 

最后看看各种help..

> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
>

 

 

MongoDB学习笔记【1】-- 安装启动

MongoDB学习笔记【2】-- 试用

MongoDB学习笔记【3】-- MongoDB C驱动使用

MongoDB学习笔记【4】-- MongoDB Java驱动使用

 

posted @ 2013-03-01 11:35  Leo Forest  阅读(364)  评论(0编辑  收藏  举报