MongoDB update数据语法【转】
在前面的文章“mongodb 查询的语法”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方便以后自己用到的时候查阅:
注:在这篇文章及上篇文章内讲的语法介绍都是在mongodb shell环境内的,和真正运用语言编程(如java,php等)使用时,在使用方法上会有一些差别,但语法(如查询条件,$in,$inc等)是一样的。
本文是参考官方文档来介绍的,之所以有官方文档还要在这介绍,一方面是就当翻译,毕竟每次要用时去看英文文档比较累,第二是官方文档讲解比较简单,有时光看官方文档不好理解,我在实际操作的情况下可以做些补充。
好了,不多说了,下面正式开始:
mongodb更新有两个命令:
1).update()命令
db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
例:
db.test0.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条记录
db.test0.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 全更新了
db.test0.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 只加进去了第一条
db.test0.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 全加进去了
db.test0.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );全更新了
db.test0.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );只更新了第一条
2).save()命令
db.collection.save( x )
x就是要更新的对象,只能是单条记录。
如
果在collection内已经存在一个和x对象相同的"_id"的记录。mongodb就会把x对象替换collection内已经存在的记录,否则将
会插入x对象,如果x内没有_id,系统会自动生成一个再插入。相当于上面update语句的upsert=true,multi=false的情况。
例:
db.test0.save({count:40,test1:"OK"}); #_id系统会生成
db.test0.save({_id:40,count:40,test1:"OK"}); #如果test0内有_id等于40的,会替换,否则插入。
mongodb的更新操作符:
1) $inc
用法:{ $inc : { field : value } }
意思对一个数字字段field增加value,例:
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 16, "test1" : "TESTTEST",
"test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $inc : { "count" : 1 } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 17, "test1" : "TESTTEST",
"test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $inc : { "count" : 2 } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 19, "test1" : "TESTTEST",
"test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $inc : { "count" : -1 } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "TESTTEST",
"test2" : "OK", "test3" : "TESTTEST", "test4" : "OK", "test5" : "OK" }
2) $set
用法:{ $set : { field : value } }
就是相当于sql的set field = value,全部数据类型都支持$set。例:
>
db.test0.update( { "_id" : 15 } , { $set : { "test1" : "testv1","test2"
: "testv2","test3" : "testv3","test4" : "testv4" } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : "testv1",
"test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" :
"OK" }
3) $unset
用法:{ $unset : { field : 1} }
顾名思义,就是删除字段了。例:
> db.test0.update( { "_id" : 15 } , { $unset : { "test1":1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test2" : "testv2", "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $unset : { "test2": 0 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test3" : "testv3", "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $unset : { "test3":asdfasf } } );
Fri May 14 16:17:38 JS Error: ReferenceError: asdfasf is not defined (shell):0
> db.test0.update( { "_id" : 15 } , { $unset : { "test3":"test" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test4" : "testv4", "test5" : "OK" }
没看出field : 1里面的1是干什么用的,反正只要有东西就行。
4) $push
用法:{ $push : { field : value } }
把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去。例:
> db.test0.update( { "_id" : 15 } , { $set : { "test1" : ["aaa","bbb"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $push : { "test1": "ccc" } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb", "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $push : { "test2": "ccc" } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb",
"ccc" ], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $push : { "test1": ["ddd","eee"] } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb",
"ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
"test5" : "OK" }
5) $pushAll
用法:{ $pushAll : { field : value_array } }
同$push,只是一次可以追加多个值到一个数组字段内。例:
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb",
"ccc", [ "ddd", "eee" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
"test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pushAll : { "test1": ["fff","ggg"] } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "aaa", "bbb",
"ccc", [ "ddd", "eee" ], "fff", "ggg" ], "test2" : [ "ccc" ], "test4" :
"testv4", "test5" : "OK" }
6) $addToSet
用法:{ $addToSet : { field : value } }
增加一个值到数组内,而且只有当这个值不在数组内才增加。例:
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"aaa",
"bbb",
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444",
"555"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": {$each : ["444","555"] } } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"aaa",
"bbb",
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444",
"555"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"aaa",
"bbb",
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444",
"555",
[
"444",
"555"
]
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $addToSet : { "test1": ["444","555"] } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"aaa",
"bbb",
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444",
"555",
[
"444",
"555"
]
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
7) $pop
删除数组内的一个值
用法:
删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }
注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用,例:
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"bbb",
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pop : { "test1": -1 } } );
> db.test0.find( { "_id" : 15 } );
{ "_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [
"ccc",
[
"ddd",
"eee"
],
"fff",
"ggg",
[
"111",
"222"
],
"444"
], "test2" : [ "ccc" ], "test4" : "testv4", "test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pop : { "test1": 1 } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [
"ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ],
"test4" : "testv4",
"test5" : "OK" }
8) $pull
用法:$pull : { field : value } }
从数组field内删除一个等于value值。例:
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [
"ddd", "eee" ], "fff", "ggg", [ "111", "222" ] ], "test2" : [ "ccc" ],
"test4" : "testv4",
"test5" : "OK" }
> db.test0.update( { "_id" : 15 } , { $pull : { "test1": "ggg" } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [
"ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" :
"testv4", "test5"
: "OK" }
9) $pullAll
用法:{ $pullAll : { field : value_array } }
同$pull,可以一次删除数组内的多个值。例:
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ "ccc", [
"ddd", "eee" ], "fff", [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" :
"testv4", "test5"
: "OK" }
> db.test0.update( { "_id" : 15 } , { $pullAll : { "test1": [ "ccc" , "fff" ] } } );
> db.test0.find( { "_id" : 15 } );
{
"_id" : { "floatApprox" : 15 }, "count" : 18, "test1" : [ [ "ddd",
"eee" ], [ "111", "222" ] ], "test2" : [ "ccc" ], "test4" : "testv4",
"test5" : "OK" }
10) $ 操作符
$是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:
> t.find()
{
"_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 3 }, { "by" : "jane", "votes" :
7 } ] }
> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, false, true )
> t.find()
{
"_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : "ABC",
"comments" : [ { "by" : "joe", "votes" : 4 }, { "by" : "jane", "votes" :
7 } ] }
需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子:
> t.find();
{ "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 2, 3, 2 ] }
> t.update({x: 2}, {$inc: {"x.$": 1}}, false, true);
> t.find();
还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例:
> t.insert({x: [1,2,3,4,3,2,3,4]})
> t.find()
{ "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
> t.update({x:3}, {$unset:{"x.$":1}})
> t.find()
{ "_id" : ObjectId("4bde2ad3755d00000000710e"), "x" : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
{ "_id" : ObjectId("4b9e4a1fc583fa1c76198319"), "x" : [ 1, 3, 3, 2 ] }
转自:http://hi.baidu.com/farmerluo/item/15ba88579b8bbb9409be17bb
MongoDB update数据语法【转】的更多相关文章
- Mongodb大数据语法大全
JSON和MONGODBJSON不止是一种交换数据的方式,也是一种存储数据的良好方式,实际上MONGODB并未使用JSON存储数据,而是使用由MONGODB团队开发的一种称为BSON的开放数据格式. ...
- 【mongodb系统学习之十二】mongodb修改数据(一)
十二.mongodb修改数据:update 1).修改数据库数据:update:语法 db.collectionName.update({},{},boolean,boolean): 2).updat ...
- Mongodb与mysql语法比较
Mongodb与mysql语法比较 mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由 ...
- Oracle Update 语句语法与性能分析 - 多表关联
Oracle Update 语句语法与性能分析 - 多表关联 为了方便起见,建立了以下简单模型,和构造了部分测试数据: 在某个业务受理子系统BSS中, SQL 代码 --客户资料表 create ...
- 【mongodb系统学习之十一】mongodb删除数据
十一.mongodb删除数据: 1).删除全部文档:remove,语法db.collectionName.remove({}):小括号里边必须要有条件,否则不成功:如果只是一个空的{},则会删除集合内 ...
- 【mongodb系统学习之九】mongodb保存数据
九.mongodb保存数据: 1).插入.保存数据:insert:语法db.collectionName.insert({"key":value}),key是字段名,必须是字符串( ...
- mongodb 创建更新语法
创建文档 向MongoDB插入数据,使用insert, 如:db.refactor.insert({"refactor's blog":"http://www.cnblo ...
- MongoDB update修改器: 针对Fields的$修改器 $inc $set $unset
MongoDB update修改器: $inc $set $unset $push $pull $pop 针对Fields的$修改器 $set: { $set: { key: value } } $s ...
- 使用node和express+mongodb实现数据增删改功能
2018即将过去,2019即将来临,前端技术不断在在更新,学的东西越来越多.我们只有不断的学习,才不能被淘汰.在前后端分离的一个时代,后端提供接口,前端调用接口,逻辑判断,每个都是独立的工作.如果自己 ...
随机推荐
- SampleManager(赛默飞)
- [C#] 常用工具类——应用程序属性信息访问类
using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...
- malloc()与calloc差别
Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slig ...
- Android-Wheel-Menu
https://github.com/anupcowkur/Android-Wheel-Menu
- 使用solrj操作solr索引库,solr是lucene服务器
客户端开发 Solrj 客户端开发 Solrj Solr是搭建好的lucene服务器 当然不可能完全满足一般的业务需求 可能 要针对各种的架构和业务调整 这里就需要用到Solrj了 Solrj是Sol ...
- ActivityGroup相关--getLocalActivityManager()
ActivityGroup简介 1.ActivityGroup的核心就是继承了该类,能够通过getLocalActivityManager()得到一个LocalActivityManager 如,Lo ...
- tabhost中setup()和setup(LocalActivityManager activityGroup)
如果用系统默认的tabhost时, 直接用getTabhost()初始化,整个类继承tabActivity. 当没有选择系统tabhost默认id时,而是自己定义的id时,必须使用 findViewB ...
- Kia's Calculation(贪心)
pid=4726">http://acm.hdu.edu.cn/showproblem.php? pid=4726 大致题意:给两个长度小于10^6且相等的合法的正整数.你能够随意组合 ...
- MYSQL 基础操作
1.MySQL基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t1; --创建一个和t1一样的表,用like(表结构也一样 ...
- HTML+CSS 整站 步骤
文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css 注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...