mongodb 入门基础知识

     

     Mongodb是面向文档的数据库,不是关系型的,将关系型数据库的Row 转换为Document(每个文档都有一个_id,ObjectId是_id的默认类型“0123(时间戳)456(机器)78(PID)91011(计数器)”),长度不固定,更加灵活,这种Key Value的存储方式也是MapReduce的一个基础。Mongodb具有很多优秀的功能,比如 索引功能,存储javascript,功能(用来替代存储过程),聚合函数的支持,固定大小的集合(capped),还有文件系统GridFS用来存储文件

    

    Mongodb的内置数据库有admin,local,config(分片时用)

    数据类类型:null,布尔,数字(没法区分浮点数和证书,32位或64位),字符串,数组,对象

null
{"x":null}

布尔
{"x":true}
字符串
{"x":"foobar"}
对象ID
{"x":ObjectId()}

日期
{"x":new Date()}

未定义
{"x":undefined}

数组
{"x":["a","b"]}

内嵌文档
{"x":{"a":1}}

    下面看下CRUD

   先新建一个Document,然后插入数据,在查询

> db.testcreate.insert({"title":"test create document","content":"this is a test
 create document"});
WriteResult({ "nInserted" : 1 })

> db.testcreate.insert({"title":"test2 create document","content":"this is a tes
t create document"});
WriteResult({ "nInserted" : 1 })

> db.testcreate.findOne();
{
        "_id" : ObjectId("546d5ad18ab176b70534278f"),
        "title" : "test create document",
        "content" : "this is a test create document"
}
>
> db.testcreate.find();
{ "_id" : ObjectId("546d5ad18ab176b70534278f"), "title" : "test create document"
, "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5b1b8ab176b705342790"), "title" : "test2 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5b2b8ab176b705342791"), "title" : "test3 create document
", "content" : "this is a test create document" }
>

   

     在看下Remove,可以删除整合集合,使用db.testcreate.drop();

   

> db.testcreate.remove({"title":"test create document"});
WriteResult({ "nRemoved" : 1 })
> db.testcreate.find();
{ "_id" : ObjectId("546d5b1b8ab176b705342790"), "title" : "test2 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5b2b8ab176b705342791"), "title" : "test3 create document
", "content" : "this is a test create document" }
>

  

   在来测试下Update,shell这样写比较麻烦,需要先定义好新文档的结构,然后在执行Update根据一定的条件   

   原来的定义

  

> db.testcreate.insert(testcreate);
WriteResult({ "nInserted" : 1 })
> db.testcreate.find();
{ "_id" : ObjectId("546d5b1b8ab176b705342790"), "title" : "test2 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5b2b8ab176b705342791"), "title" : "test3 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5d398ab176b705342792"), "title" : "thisisatest", "conten
t" : "hereis my blog post", "date" : ISODate("2014-11-20T03:16:58.521Z") }
>

   修改

   

> testcreate ={"title":"thisisatest","content":"hereis my blog post","date":new
Date()}
{
        "title" : "thisisatest",
        "content" : "hereis my blog post",
        "date" : ISODate("2014-11-20T03:20:14.868Z")
}
> testcreate.comments =[];
[ ]
> db.testcreate.update({"title":"thisisatest"},testcreate);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testcreate.find();
{ "_id" : ObjectId("546d5b1b8ab176b705342790"), "title" : "test2 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5b2b8ab176b705342791"), "title" : "test3 create document
", "content" : "this is a test create document" }
{ "_id" : ObjectId("546d5d398ab176b705342792"), "title" : "thisisatest", "conten
t" : "hereis my blog post", "date" : ISODate("2014-11-20T03:20:14.868Z"), "comme
nts" : [ ] } 
>

   修改是个复杂的操作,为了应对复杂的需求,Mongo提供了一些修改器来提高修改的效率,主要有$inc递增, $set 修改器,用来修改指定Key的值,如果不存在,那么创建它,

    数组修改器:$push,$pull ,$ 定位符

   upsert:没有符合的文档则创建,有则更新

另外,在更新的时候不能更新_id,只有当整合文档都替换的时候才可以更新_id,由于MongoDb默认是异步修改数据的,那么想要知道更新是否成功可以使用getLastError来获得,更近一步,使用findAndModify来获取修改后的记录。

  

猜你喜欢

转载自chenhua-1984.iteye.com/blog/2158407