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..
> 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學習筆記【1】-- 安裝啟動
MongoDB學習筆記【2】--?試用
MongoDB學習筆記【3】--?MongoDB C驅動使用
MongoDB學習筆記【4】-- MongoDB Java驅動使用
?
轉載于:https://www.cnblogs.com/Leo-Forest/archive/2013/03/01/2938364.html
總結
以上是生活随笔為你收集整理的MongoDB学习笔记【2】-- 试用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: session may be lost
- 下一篇: Windows Phone 7.1 Se