1、mongo服务的启动:./mongod或者mogod.exe
2、shell的启动:./mongo
在启动shell之前必须启动服务
3、shell的简单使用
可以进行简单的数学运算:
> x = 200
200
> x / 5;
40
可以使用标准的javascript标准库,api可以参考此连接:http://api.mongodb.org/js/:
> Math.sin(Math.PI / 2);
1
> new Date("2010/1/1");
"Fri Jan 01 2010 00:00:00 GMT-0500 (EST)"
> "Hello, World!".replace("World", "MongoDB");
Hello, MongoDB!
甚至可以定义javascript函数:
> function factorial (n) {
... if (n <= 1) return 1;
... return n * factorial(n - 1);
... }
> factorial(5);
120
4、shell基本操作
(1)Create:使用insert函数往collections中插入document
> use test //如果不存在test数据库,将会被创建,如果在此次会话中没有对test做任何操作,它将被丢弃
switched to db test
> post={"title" : "My Blog Post",
... "content" : "Here's my blog post.",
... "date" : new Date()}
{
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"date" : ISODate("2011-10-07T03:06:02.953Z")
}
> db.blog.insert(post) //自动创建blog的collection
使用find方法将所有记录检索出来
> db.blog.find()
{ "_id" : ObjectId("4e8d7c5514000d6abf16d4de"), "title" : "ttt", "content" : "he
llo world!" }
{ "_id" : ObjectId("4e8e6cb383be5fbc5a587f5d"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2011-10-07T03:06:02.953Z") }
(2)Update:使用update方法更新collection
> post.comments=[]
[ ]
> db.blog.update({title:"My Blog Post"}, post)
> db.blog.find()
{ "_id" : ObjectId("4e8d7c5514000d6abf16d4de"), "title" : "ttt", "content" : "he
llo world!" }
{ "_id" : ObjectId("4e8e6cb383be5fbc5a587f5d"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2011-10-07T03:06:02.953Z"), "com
ments" : [ ] }
(3)Delete:使用remove方法删除document
> db.blog.remove({"title":"ttt"})
> db.blog.find()
{ "_id" : ObjectId("4e8e6cb383be5fbc5a587f5d"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2011-10-07T03:06:02.953Z"), "com
ments" : [ ] }