• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 4年前 (2022-01-09) 14次浏览 已收录 0个评论

第三章创建、更新及删除文档插入新文档:db.foo.insert({bar:baz})我的理解:数据库.集合.插入({key:value})注:指插入速度比单次插入数据要快,因为批量插入只

第三章
创建、更新及删除文档

插入新文档:

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(“502e9c960852475a6e43ba本文来源gaodai#ma#com搞*!代#%^码$网!78″)} ,
… {“$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)

返回更新文档:

本文出自 “煮酒品茶” 博客,谢绝转载!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MongoDB笔记二之创建、更新及删除文档

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址