煮酒品茶:大量序语和实例引用自书中,品茶对其实验后做的笔记上载至博客,任何有版权的人都可以直接下架下文章,谢谢合作。
{"cwtea" : "hello","key":"vlaue"} |
1、文档中的键/值对应是有序的
{"cwtea" : "hello","key":"vlaue"}和{"key":"vlaue","cwtea" : "hello"}完全不同
2、文档中的值可以是多种数据类型,整字,字符串,甚至文档。键可以使用任意UTF-8字符。
3、键不能含有\0(空字符),这个字符用来表示键的结尾。
4、.和$有特别的意义,只有在特定环境下才能使用。
5、以下划线"_"开头的键是保留的,虽然这个并不是严格要求的。
6、mongoDB区分类型和区分大小写
{"foo" : 3} 不同于{"foo" : "3"} 整数和字符串
{ "foo" : "3"}不同于{"Foo" : "3"} 大小写
7、mongoDB不能有重复的键
|
> math.sin(math.pi /2)
Mon Aug 13 23:28:19 ReferenceError: math is not defined (shell):1
> Math.sin(Math.PI /2)
1
> new Date("2010/1/1")
ISODate("2009-12-31T16:00:00Z")
> new Date("2010/1/1");
ISODate("2009-12-31T16:00:00Z")
> "hello ,world!".replace("world", "mongodb")
hello ,mongodb!
> "hello ,world!".replace("world1", "mongodb")
hello ,world!
> "hello ,2".replace("2", "mongodb")
hello ,mongodb
> function factorial (n) {
... if (n <=1) return 1;
... return n * factorial(n-1)
... }
> factorial(5);
120
|
> use blog
switched to db blog
> post = {"title" : "my blog post",
... "content" : "here is my blog post.",
... "date" : new Date()}
{
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z")
}
> db.blog.insert(post)
> db.blog.find()
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post", "content" : "here is my blog post.", "date" : ISODate("2012-08-13T16:24:51.467Z") }
|
> db.blog.findOne()
{
"_id" : ObjectId("50292a7870fc23d6b7514586"),
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z")
}
> db.blog.find({},{"title": 1} )
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post" }
{ "_id" : ObjectId("50292b1a70fc23d6b7514588"), "title" : "two" }
|
> post
{
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z")
}
> post.comments =[]
[ ]
> post
{
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z"),
"comments" : [ ]
}
> post.test="hello"
hello
> post
{
"title" : "my blog post",
"content" : "here is my blog post.",
"date" : ISODate("2012-08-13T16:24:51.467Z"),
"comments" : [ ],
"test" : "hello"
}
#替换标题为my blog post的文章,第一个为条件,第二个为更新内容。
> db.blog.update({title : "my blog post"}, post)
> db.blog.find()
{ "_id" : ObjectId("50292b1a70fc23d6b7514588"), "title" : "two", "content" : ISODate("2012-08-13T16:28:10.372Z") }
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post", "content" : "here is my blog post.", "date" : ISODate("2012-08-13T16:24:51.467Z"), "comments" : [ ], "test" : "hello" }
> db.blog.update({title : "two"}, {"title" : "baidu","content" : "www.cwtea.com" })
> db.blog.find()
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post", "content" : "here is my blog post.", "date" : ISODate("2012-08-13T16:24:51.467Z"), "comments" : [ ], "test" : "hello" }
{ "_id" : ObjectId("50292b1a70fc23d6b7514588"), "title" : "baidu", "content" : "www.cwtea.com" }
|
> db.blog.remove({"title" : "baidu"})
> db.blog.find()
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post", "content" : "here is my blog post.", "date" : ISODate("2012-08-13T16:24:51.467Z"), "comments" : [ ], "test" : "hello" }
|
> lq = { "name" : "cwtea",
... "address" : {
... "a" : "1",
... "b" : "2",
... "c" : "3",
... }
... ,"hello" :"cwtea"}
{
"name" : "cwtea",
"address" : {
"a" : "1",
"b" : "2",
"c" : "3"
},
"hello" : "cwtea"
}
> db.blog.insert(lq)
> db.blog.find()
{ "_id" : ObjectId("50292a7870fc23d6b7514586"), "title" : "my blog post", "content" : "here is my blog post.", "date" : ISODate("2012-08-13T16:24:51.467Z"), "comments" : [ ], "test" : "hello" }
{ "_id" : ObjectId("5029363070fc23d6b7514589"), "name" : "cwtea", "address" : { "a" : "1", "b" : "2", "c" : "3" }, "hello" : "cwtea" }
>
|
0-3[时间戳]4-6[机器]7-8[pid]9-11[计数器] |