日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/21 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MongoDB学习笔记【2】-- 试用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

大部分內(nèi)容根據(jù)MongoDB官方手冊整理:http://docs.mongodb.org/manual/contents/

查看數(shù)據(jù)庫

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

使用use切換數(shù)據(jù)庫,use一個新名字可以使用新數(shù)據(jù)庫,MongoDB會等到插入數(shù)據(jù)才會建立

> 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看內(nèi)容

> 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不支持多個參數(shù)

用循環(huán)插入

> 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 } >

繼續(xù)插入

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

查看當(dāng)前數(shù)據(jù)庫 db 刪除數(shù)據(jù)庫 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

?

使用游標(biāo)和循環(huán)來打印內(nèi)容

> 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() 返回游標(biāo)是否指向文件,next 返回下一個文件。printjson 把文件用用類json格式打印出來。

使用游標(biāo)的下標(biāo)

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

?

打印復(fù)雜點的json數(shù)據(jù)

> 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..

> helpdb.help() help on db methodsdb.mycoll.help() help on collection methodssh.help() sharding helpersrs.help() replica set helpershelp admin administrative helphelp connect connecting to a db helphelp keys key shortcutshelp misc misc things to knowhelp mr mapreduceshow dbs show database namesshow collections show collections in current databaseshow users show users in current databaseshow profile show most recent system.profile entries with time >= 1msshow logs show the accessible logger namesshow log [name] prints out the last segment of log in memory, 'global' is defaultuse <db_name> set current databasedb.foo.find() list objects in collection foodb.foo.find( { a : 1 } ) list objects in foo where a == 1it result of the last line evaluated; use to further iterateDBQuery.shellBatchSize = x set default number of items to display on shellexit quit the mongo shell >

?

?

MongoDB學(xué)習(xí)筆記【1】-- 安裝啟動

MongoDB學(xué)習(xí)筆記【2】--?試用

MongoDB學(xué)習(xí)筆記【3】--?MongoDB C驅(qū)動使用

MongoDB學(xué)習(xí)筆記【4】-- MongoDB Java驅(qū)動使用

?

轉(zhuǎn)載于:https://www.cnblogs.com/Leo-Forest/archive/2013/03/01/2938364.html

總結(jié)

以上是生活随笔為你收集整理的MongoDB学习笔记【2】-- 试用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。