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”)来访问

posted @ 2013-06-08 10:13  张巍的博客  阅读(10237)  评论(0编辑  收藏  举报