`

mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)

阅读更多

对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能够高效的进行文档更新。更新修改器是中特殊的键,
用来指定复杂的操作,比如增加、删除或者调整键,还可能是操作数组或者内嵌文档。

 

1.$inc
--------------------------------------------------------------------------
这个修改器干什么使的呢?看看下面示例的具体操作后的结果即可知道。

 

示例文档:{"uid":"201203","type":"1",size:10}

 

> db.b.insert({"uid":"201203","type":"1",size:10})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 10 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 11 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 2}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 13 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : -1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 12 }

 

得出结论:修改器$inc可以对文档的某个值为数字型(只能为满足要求的数字)的键进行增减的操作。
(这里有个问题:上篇中说到更新默认只对满足条件的记录集中第一个文档进行更新,那么使用$inc修改器之后,还是一样吗?)

 

2.$set
-------------------------------------------------------------------
用来指定一个键并更新键值,若键不存在并创建。来看看下面的效果:

 

> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--size键不存在的场合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"size":10}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--sname键存在的场合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
--可改变键的值类型
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":["Java",".net","c++"]}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "num" : 40,
        "size" : 10,
        "sname" : [
                "java",
                ".net",
                "c++"
        ],
        "type" : "3",
        "uid" : "20120002"
}

 

对于内嵌的文档,$set又是如何进行更新的内嵌的文档的呢,请看下面的示例:
示例文档:{"name":"toyota","type":"suv","size":{"height":10,"width":5,"length":15}}

 

> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 10,
                "width" : 5,
                "length" : 15
        }
}
> db.c.update({"name":"toyota"},{"$set":{"size.height":8}})
> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 8,
                "width" : 5,
                "length" : 15
        }
}
> db.c.update({"name":"toyota"},{"$set":{"size.width":7}})
> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 8,
                "width" : 7,
                "length" : 15
        }
}
可见:对于内嵌文档在使用$set更新时,使用"."连接的方式。

 


3.$unset
----------------------------------------------------------------
从字面就可以看出其意义,主要是用来删除键。
示例操作效果如下:

 

> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"sname":1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "num" : 40,
        "size" : 10,
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"num":0}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "size" : 10,
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"size":-1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"desc":"sssssss"}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "type" : "3",
        "uid" : "20120002"
}

 

得出结论:使用修改器$unset时,不论对目标键使用1、0、-1或者具体的字符串等都是可以删除该目标键。

 

4.数组修改器--$push
------------------------------------------------------------------
示例操作效果如下:
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv",
"size" : { "height" : 8, "width" : 7, "length" : 15 } }

 

--先push一个当前文档中不存在的键title
> db.c.update({"name" : "toyota"},{$push:{"title":"t1"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1" ], "type" : "suv" }
 
--再向title中push一个值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2" ], "type" : "suv" }

 

--再向title中push一个值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

 

--再向一个已经存在的键值非数组类型的键push一个值
> db.c.update({"name" : "toyota"},{$push:{"size.height":10}})
Cannot apply $push/$pushAll modifier to non-array
> db.c.update({"name" : "toyota"},{$push:{"name":"ddddddd"}})
Cannot apply $push/$pushAll modifier to non-array

 

得出结论:$push--向文档的某个数组类型的键添加一个数组元素,不过滤重复的数据。添加时键存在,要求键值类型必须是数组;键不存在,则创建数组类型的键。

 

5.数组修改器--$ne/$addToSet
---------------------------------------------------------------------
主要给数组类型键值添加一个元素时,避免在数组中产生重复数据,$ne在有些情况是不通行的。

 

> db.c.update({"title" : {$ne:"t2"}},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

 

> db.c.update({"name" : "toyota"},{$addToSet:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

 

6.数组修改器--$pop、$pull
------------------------------------------------------------
$pop从数组的头或者尾删除数组中的元素,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3", "t4" ],"type" : "suv" }

 

--从数组的尾部删除 1
> db.c.update({"name" : "toyota"},{$pop:{"title":1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3" ], "type" : "suv" }
--从数组的头部 -1
> db.c.update({"name" : "toyota"},{$pop:{"title":-1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t2", "t3" ], "type" : "suv" }
--从数组的尾部删除 0
> db.c.update({"name" : "toyota"},{$pop:{"title":0}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t2" ], "type" : "suv" }
 
$pull从数组中删除满足条件的元素,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2", "t3" ],"type" : "suv" }
 
> db.c.update({"name" : "toyota"},{$pull:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t3" ], "type" : "suv" }
 
7.数组的定位修改器
-------------------------------------------------------------------
在需要对数组中的值进行操作的时候,可通过位置或者定位操作符("$").数组是0开始的,可以直接将下标作为键来选择元素。
示例如下:
{"uid":"001",comments:[{"name":"t1","size":10},{"name":"t2","size":12}]}

 

> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 10 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"uid":"001"},{$inc:{"comments.0.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 11 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"comments.name":"t1"},{$set:{"comments.$.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 1 }, { "name" : "t2", "size" : 12 } ] }

 

--若为多个文档满足条件,则只更新第一个文档。

 

8.upsert
-----------------------------------------------------------------
upsert是一种特殊的更新。当没有符合条件的文档,就以这个条件和更新文档为基础创建一个新的文档,如果找到匹配的文档就正常的更新。
使用upsert,既可以避免竞态问题,也可以减少代码量(update的第三个参数就表示这个upsert,参数为true时)

 

> db.c.remove()
> db.c.update({"size":11},{$inc:{"size":3}})
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},false)
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},true)
> db.c.find()
{ "_id" : ObjectId("5003ded6c28f67507a6df1de"), "size" : 14 }

9.save函数
-----------------------------------------------------------------
1.可以在文档不存在的时候插入,存在的时候更新,只有一个参数文档。
2.要是文档含有"_id",会调用upsert。否则,会调用插入。
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 50,
 "sname" : "jk", "type" : "1", "uid" : "20120002" }
> var o = db.a.findOne()
> o.num = 55
55
> db.a.save(o)
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 55,
 "sname" : "jk", "type" : "1", "uid" : "20120002" }

分享到:
评论

相关推荐

    mongodb_exporter监控 https://github.com/dcu/mongodb_exporter.git 下19年4月的编译结果文件

    mongodb_exporter监控 https://github.com/dcu/mongodb_exporter.git 下19年4月的编译结果文件 编译很麻烦 ,注意修改文件的可执行权限

    mongodb 修改器($inc/$set/$unset/$push/$pop/upsert)

    $inc 修改器用于对文档中的数值字段进行增加或减少操作。如在示例中所示,当你想增加或减少一个数值字段的值时,你可以使用 $inc 关键字。例如,`db.b.update({"uid" : "201203"}, {"$inc": {"size": 1}})` 将会...

    Data-Unit_MongoDB_Restore_v2.1.zip

    Data-Unit_MongoDB_Restore适用于磁盘存储故障、勒索病毒删除、误操作删除等导致的数据库丢失恢复。运行工具后将数据文件拖入本窗口即可。也支持扫描分区和裸磁盘,建议扫描磁盘镜像文件,效果更好。 免费版每个集合...

    Linux安装mongodb客户端

    sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo 写入: [mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/ gpgcheck=1 enabled=1 gpg...

    org.mongodb.spark:mongo-spark-connector_2.11:1.1.0

    mongodb-spark官方连接器,运行spark-submit --packages org.mongodb.spark:mongo-spark-connector_2.11:1.1.0可以自动下载,国内网络不容易下载成功,解压后保存到~/.ivy2目录下即可。

    mongodb_java_2.6_API

    更新操作可以通过`updateMany()`或`updateOne()`方法完成,可以使用各种更新操作符如`$inc`(增量更新)、`$set`(设置字段值)等。 ```java collection.updateMany(filter, new Document("$inc", new Document...

    解决Linux上MongoDB启动脚本错误---env: /etc/init.d/mongodb : no such file or directory

    在Linux系统中,MongoDB是一个常用的文档型数据库管理系统,它为开发者提供了高性能、高可用性和易扩展性的数据存储解决方案。然而,在使用过程中,有时会遇到启动脚本错误,如"env: /etc/init.d/mongodb : no such ...

    The MongoDB Database

    Components mongod - The database ...$ ./mongod --help To run a single server database: $ sudo mkdir -p /data/db $ ./mongod $ $ # The mongo javascript shell connects to localhost and test database by

    MongoDB_Symfony_User_Reg

    MongoDB_Symfony_User_Reg 该项目是使用Docker环境,带有MongoDB和Symfony的NoSql数据库的基本Register,Login / Logout应用程序。 请执行以下步骤:克隆项目$ git clone ...

    centos上安装使用Mongodb及mongoPHP扩展

    - 使用 `./start.sh` 启动 MongoDB,`./stop.sh` 关闭 MongoDB。 4. **安装 MongoDB PHP 扩展** - 对于 PHP 的 MongoDB 支持,你需要安装 PHP 的 MongoDB 扩展。你可以使用 PECL 来完成这个任务: ``` pecl ...

    linux mongoDB安装及配置.docx

    [root@localhost bin]# ./mongod --dbpath=/data/mongodb_data/ --logpath=/data/mongodb_log/mongodb.log --logappend & ``` 3. 查看 MongoDB 是否启动成功:使用 `netstat` 命令查看端口号 27017 是否已经启动。 `...

    centos系统php 5.6对应的mongo.so下载及源码

    $ mv mongodb-mongo-1.6.12/ /usr/local/mongodb $ /usr/local/php/bin/phpize $ ./configure --with-php-config=/usr/local/php/bin/php-config $ make all -j 5 $ sudo make install 命令执行完之后 出现的...

    mongodb-org-server-4.4.0-1.el7.x86_64.rpm

    官网下载的:mongodb-org-server-4.4.0-1.el7.x86_64.rpm 官网速度慢,此处放一份。 下载地址: https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.0-1.el7.x86_64.rpm 无...

    Linux下安装MongoDB.docx

    MongoDB 是一个高性能、开源、无模式的分布式文档数据库,被广泛用于开发现代应用程序,特别是那些需要处理大量非结构化数据的应用程序。在 Linux 系统中安装 MongoDB 可以按照以下步骤进行: 1. **下载安装包**: ...

    mongodb_linux.zip

    MongoDB是一款开源、高性能、无模式的分布式文档型数据库,被广泛应用于数据分析、内容管理系统、物联网(IoT)以及各种Web和移动应用中。在Linux系统中安装和使用MongoDB,能够充分利用Linux的稳定性和安全性。本...

    安装MongoDB.pdf

    解压文件:将下载的安装包解压到你想安装的位置,例如C:\Program Files\MongoDB 或者 /usr/local/mongodb。 配置环境变量: 对于Windows用户,可以在系统变量 Path 中添加 mongod 的路径。 对于Linux或macOS用户,...

    mongodb-win32-x86_64-windows-4.4.5.zip(MongoDB + Robot 3T连接工具,包含一键添加服务、一键启/关闭脚本)

    4.4.5 MongoDB + 1.4.3 Robot 3T连接工具,官网下载,配置已初始化好,包含一键添加服务、一键启动、关闭等脚本。使用方便,亲测好用。配套文章:https://blog.csdn.net/qq_33204709/article/details/115905659

    MongoDB_MongoDB_

    这可以通过Visual Studio的包管理器控制台或使用dotnet CLI命令`dotnet add package MongoDB.Driver`完成。 2. **连接到MongoDB服务器** 使用MongoClient类可以建立与MongoDB服务器的连接。你需要提供一个连接字符...

Global site tag (gtag.js) - Google Analytics