MongoDB shell


MongoDB自帶了一個javascript shell,可以從命令行與MongoDB交互

運行

運行mongo 啟動shell

我在運行的時候出現了這樣的錯誤:不能連接到服務

:\mongodb\mongodb-win32-i386-2.4.4\bin>mongo
ongoDB shell version: 2.4.4
onnecting to: test
at Jun 08 09:01:36.048 JavaScript execution failed: Error: couldn't connect to
erver 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
xception: connect failed

昨天我們設置了自啟動,服務沒有開啟,這里顯示的是自動,服務卻是停止的,進系統服務界面開啟

image

在輸入一次mongo,進入到了shell

D:\mongodb\mongodb-win32-i386-2.4.4\bin>mongo
MongoDB shell version: 2.4.4
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
Sat Jun 08 09:05:42.766 [initandlisten]
Sat Jun 08 09:05:42.766 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary
.
Sat Jun 08 09:05:42.766 [initandlisten] **       32 bit builds are limited to le
ss than 2GB of data (or less with --journal).
Sat Jun 08 09:05:42.766 [initandlisten] **       Note that journaling defaults t
o off for 32 bit and is currently off.
Sat Jun 08 09:05:42.766 [initandlisten] **       See http://dochub.mongodb.org/c
ore/32bit
Sat Jun 08 09:05:42.766 [initandlisten]
>

 

MongoDBshell是一個功能完備的javascript的解釋器,可以運行任何的js的程序,包括函數

 

> x=100
100
> x/5
20
> Math.sin(Math.PI/2)
1
> new Date("2010/1/1")
ISODate("2009-12-31T16:00:00Z")
> "zhangsan".replace("zhang","")
san
> function foo(n){if(n==1)  return 1; return n*foo(n-1);}
> foo(1)
1
> foo(2)
2
> foo(3)
6
> foo(4)
24
>

上面我們定義了一個x的變量 ,並做了除法,調用了一個數學函數,創造了一個時間對象,定義了一個階乘函數foo並調用了它

 

MongoDB客戶端

shell能夠運行任意的javascript,同時它也是一個獨立的MongoDB客戶端

在開啟的時候會連接到MongoDB的test數據庫,並將這個連接賦值給一個全局變量db,這個變量是shell訪問MongoDB的主要入口

在shell界面輸入db,發現db是指向了test的

> db
test
>

選擇數據庫 use [databese name]

> use admin
switched to db admin
> db
admin
> use test
switched to db test
> db
test
>

創建

假設我們有一個用戶表user,包含了username password  tel

> user={username:'zhangsan',password:'1223456',tel:'123456'}
{ "username" : "zhangsan", "password" : "1223456", "tel" : "123456" }
> db.user.insert(user)
> db.user.find()
{ "_id" : ObjectId("51b28b7ab73ec06e42c91596"), "username" : "zhangsan", "passwo
rd" : "1223456", "tel" : "123456" }
>


這里我們定義了一個用戶數據,並用insert插入到了user文檔中,最后用find()查詢了出來。出來一條記錄,還多了一個_id的值,這個是系統創建的唯一鍵

查詢

上面我們用了find查詢,find會查詢出所有的記錄,如果只想查詢一條記錄,可以使用findOne來查詢

> db.user.find()
{ "_id" : ObjectId("51b28b7ab73ec06e42c91596"), "username" : "zhangsan", "passwo
rd" : "1223456", "tel" : "123456" }
> db.user.findone()
Sat Jun 08 09:43:33.578 JavaScript execution failed: TypeError: Property 'findon
e' of object test.user is not a function
> db.user.findOne()
{
        "_id" : ObjectId("51b28b7ab73ec06e42c91596"),
        "username" : "zhangsan",
        "password" : "1223456",
        "tel" : "123456"
}
>


在上面我們使用了findone出現了錯誤,這里說明shell是區分大小寫的。注意一下

更新

假如我們需要更新密碼,我們在插入一條記錄,然后更新用戶為zhangsan的數據

> db.user.find()
{ "_id" : ObjectId("51b28f62b73ec06e42c91597"), "username" : "zhangsan", "passwo
rd" : "123123", "tel" : "121212" }
{ "_id" : ObjectId("51b28f74b73ec06e42c91598"), "username" : "wangwu", "password
" : "121212", "tel" : "121212" }
> db.user.update({username:'zhangsan'},{$set:{password:'abcd'}})
> db.user.find()
{ "_id" : ObjectId("51b28f62b73ec06e42c91597"), "password" : "abcd", "tel" : "12
1212", "username" : "zhangsan" }
{ "_id" : ObjectId("51b28f74b73ec06e42c91598"), "username" : "wangwu", "password
" : "121212", "tel" : "121212" }
>

刪除

刪除名稱為zhangsan的用戶

> db.user.remove({username:'zhangsan'});
> db.user.find()
{ "_id" : ObjectId("51b28f74b73ec06e42c91598"), "username" : "wangwu", "password
" : "121212", "tel" : "121212" }
>

 

注意

使用db.集合名的方式來訪問沒有什么不妥,但是如果集合名剛好和db的屬性相同就會有問題了,只有當javascript在集合名中找不到屬性的時候才會返回集合。當目標屬性和集合名相同的時候可以使用getCollection函數,包括中間有符號的比如一個名為a-b的集合會被js認為是a減b,這個時候也可以用getCollection(“a-b”)來訪問


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM