您的位置:首页 > 数据库 > Mongodb

MongoDB笔记二之创建、更新及删除文档 推荐

2012-09-05 11:56 316 查看
第三章

创建、更新及删除文档

插入新文档:

db.foo.insert({"bar" : "baz"})
我的理解:数据库.集合.插入({key : value})

注:指插入速度比单次插入数据要快,因为批量插入只是单个TCP请求,避免了许多零碎的请求所带来的开销。(单集合)

MongoDB2.0消息长度为16MB

过程:执行插入后,使用的驱动程序会将数据转换成BSON的形式,然后将其送入数据库,数据库解析BSON,检验是否包含"_id"键并且文档不超过4MB,

注:所有主流语言的驱动会在传送数据之前进行一些数据的有效性检查(文档是否超长,是否包含UTF-8字符,或者使用了未知类型)。 可以在启动数据库服务器时使用--objcheck选项,服务器就会在插入之前先检查文档结构的有效性。

删除文档:

db.users.remove()
数据库.集合.删除()

注:会删除users集合中所有的文档,但不会删除集合本身,原有的索引也会保留。

remove()函数查接受一个查询文档作为可选参数。如

db.users.remove({"key" : "world"})
注:删除数据是永久性的,不能撤消,也不能恢复。

更新文档:update:两个参数,查询文档和修改器文档。

使用修改器:

通常文档只会有一部分要更新,利用原子的更新修改器,可以使得这种部分更新极为高效。更新修改器是种特殊的键,用来指定复杂的更新操作,比如调整,比如调整、增加或删除键,还可能是操作数组或者内嵌文档。

$set

以条件PD进行增加

> db.users.insert({"set" : "set"})

>

>

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "set" : "set" }

>

>

> db.users.update({ "_id" : ObjectId("502e9c960852475a6e43ba78")} ,

... {"$set" : {"hello2" : "hello2"}}

... )

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "hello2" : "hello2", "set" : "set" }

> db.users.update({ "_id" : ObjectId("502e9c960852475a6e43ba78")} ,

... {"$set" : {"nohello" : "nohello"}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "hello2" : "hello2", "nohello" : "nohello", "set" : "set" }

> db.users.update({"hello2" : "hello2"} ,

... {"$set" : {"a" : "a"}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "a" : "a", "hello2" : "hello2", "nohello" : "nohello", "set" : "set" }
$unset 删除

> db.users.update({"set" : "set"},

... {"$unset" : {"a" : "a"}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "hello2" : "hello2", "nohello" : "nohello", "set" : "set" }

注意:只PD一个值就删除这个key/value

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "hello2" : "hello2", "nohello" : "nohello", "set" : "set" }

> db.users.update({"set" : "set"},

... {"$unset" : {"hello2" : "nohello"}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "nohello" : "nohello", "set" : "set" }
同样文档中的文档也适应如{"author.name" : "???"}

一定要使用以$开头的修改器来修改键/值对。

增加和减少:

$inc

查看afs的值,只能为数字。

> db.users.update({"set" : "set"} , {"$inc" : {"afs" : 10}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "afs" : 10, "nohello" : "nohello", "set" : "set" }

> db.users.update({"set" : "set"} , {"$inc" : {"afs" : 50}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "afs" : 60, "nohello" : "nohello", "set" : "set" }

> db.users.update({"set" : "set"} , {"$inc" : {"afs" : -50}})

> db.users.find()

{ "_id" : ObjectId("502e9c960852475a6e43ba78"), "afs" : 10, "nohello" : "nohello", "set" : "set" }
数组修改器:

$push

有就加到末尾,没有就创建新数组。

数据操作,只能用在值为数组的键上,例如不能对整数做push,也不能对字符串做POP,使用"$set"或"$inc"来修改标量值。

操作过程一样

$ne $addToSet避免重复

数组的定位修改器

upsert

是一种特殊的更新,要是没有文档符合更新条件,就会以这个条件和更新文档为基础创建一个新的文档,如果找到了匹配的文档,则正常更新,upsert非常方便,不必预置集合,同一套代码可以既创建又更新文档。

save Shell帮助程序

save是一个shell函数,可以在文档不存在时插入,存在时更新,它只有一个参数:文档,要是这个文档含有"_id"键,save会调用upsert。否则,会调用插入。程序员可以非常方便地使用这个函数在shell中快速修改文档。

更新多个文档:

>db.users.update({"sr" : "10/13/1978"}, {$set : {"gift" : "happy"}},false, true)
返回更新文档:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息