傻眼了,擦,竟然开启不了,仔细观察“划线区域“的信息,发现db文件夹下有一个类似的”lock file”阻止了mongodb的开启,接下来我们要做的就
是干掉它,之后,开启成功,关于mongodb的管理方式将在后续文章分享。
一: Insert操作
上一篇也说过,文档是采用“K-V”格式存储的,如果大家对JSON比较熟悉的话,我相信学mongodb是手到擒来,我们知道JSON里面Value
可能是“字符串”,可能是“数组”,又有可能是内嵌的一个JSON对象,相同的方式也适合于BSON。
常见的插入操作也就两种形式存在:“单条插入”和“批量插入”。
① 单条插入
先前也说了,mongo命令打开的是一个javascript shell。所以js的语法在这里面都行得通,看起来是不是很牛X。
② 批量插入
这玩意跟“单条插入”的差异相信大家应该知道,由于mongodb中没有提供给shell的“批量插入方法”,没关系,各个语言的driver都打通
了跟mongodb内部的批量插入方法,因为该方法是不可或缺的,如果大家非要模拟下批量插入的话,可以自己写了for循环,里面就是insert。
二:Find操作
日常开发中,我们玩查询,玩的最多的也就是二类:
①: >, >=, <, <=, !=, =。
②:And,OR,In,NotIn
这些操作在mongodb里面都封装好了,下面就一一介绍:
<1>"$gt", "$gte", "$lt", "$lte", "$ne", "没有特殊关键字",这些跟上面是一一对应的,举几个例子。
<2> "无关键字“, "$or", "$in","$nin" 同样我也是举几个例子
<3> 在mongodb中还有一个特殊的匹配,那就是“正则表达式”,这玩意威力很强的。
<4> 有时查询很复杂,很蛋疼,不过没关系,mongodb给我们祭出了大招,它就是$where,为什么这么说,是因为$where中的value
就是我们非常熟悉,非常热爱的js来助我们一马平川。
三:Update操作
更新操作无非也就两种,整体更新和局部更新,使用场合相信大家也清楚。
<1> 整体更新
不知道大家可还记得,我在上一篇使用update的时候,其实那种update是属于整体更新。
<2> 局部更新
有时候我们仅仅需要更新一个字段,而不是整体更新,那么我们该如何做呢?easy的问题,mongodb中已经给我们提供了两个
修改器: $inc 和 $set。
① $inc修改器
$inc也就是increase的缩写,学过sql server 的同学应该很熟悉,比如我们做一个在线用户状态记录,每次修改会在原有的基础上
自增$inc指定的值,如果“文档”中没有此key,则会创建key,下面的例子一看就懂。
② $set修改器
啥也不说了,直接上代码
<3> upsert操作
这个可是mongodb创造出来的“词”,大家还记得update方法的第一次参数是“查询条件”吗?,那么这个upsert操作就是说:如果我
没有查到,我就在数据库里面新增一条,其实这样也有好处,就是避免了我在数据库里面判断是update还是add操作,使用起来很简单
将update的第三个参数设为true即可。
<4> 批量更新
在mongodb中如果匹配多条,默认的情况下只更新第一条,那么如果我们有需求必须批量更新,那么在mongodb中实现也是很简单
的,在update的第四个参数中设为true即可。例子就不举了。
在前面的文章“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" }
<strong>7) $pop</strong>
删除数组内的一个值
用法:
删除最后一个值:{ $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" }
<strong>8) $pull</strong>
用法:$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" }
<strong>9) $pullAll</strong>
用法:{ $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" }
<strong>10) $ 操作符
</strong>
$是他自己的意思,代表按条件找出的数组里面某项他自己。呵呵,比较坳口。看一下官方的例子:
> t.find()
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4b97e62bf1d8c7152c9ccb74"</span>), <span class="code-quote">"title"</span> : <span class="code-quote">"ABC"</span>, <span class="code-quote">"comments"</span> : [ { <span class="code-quote">"by"</span> : <span class="code-quote">"joe"</span>, <span class="code-quote">"votes"</span> : 3 }, { <span class="code-quote">"by"</span> : <span class="code-quote">"jane"</span>, <span class="code-quote">"votes"</span> : 7 } ] }
> t.update( {'comments.by':'joe'}, {$inc:{'comments.$.votes':1}}, <span class="code-keyword">false</span>, <span class="code-keyword">true</span> )
> t.find()
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4b97e62bf1d8c7152c9ccb74"</span>), <span class="code-quote">"title"</span> : <span class="code-quote">"ABC"</span>, <span class="code-quote">"comments"</span> : [ { <span class="code-quote">"by"</span> : <span class="code-quote">"joe"</span>, <span class="code-quote">"votes"</span> : 4 }, { <span class="code-quote">"by"</span> : <span class="code-quote">"jane"</span>, <span class="code-quote">"votes"</span> : 7 } ] }
需要注意的是,$只会应用找到的第一条数组项,后面的就不管了。还是看例子:
> t.find();
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4b9e4a1fc583fa1c76198319"</span>), <span class="code-quote">"x"</span> : [ 1, 2, 3, 2 ] }
> t.update({x: 2}, {$inc: {<span class="code-quote">"x.$"</span>: 1}}, <span class="code-keyword">false</span>, <span class="code-keyword">true</span>);
> t.find();
还有注意的是$配合$unset使用的时候,会留下一个null的数组项,不过可以用{$pull:{x:null}}删除全部是null的数组项。例:
> t.insert({x: [1,2,3,4,3,2,3,4]})
> t.find()
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4bde2ad3755d00000000710e"</span>), <span class="code-quote">"x"</span> : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
> t.update({x:3}, {$unset:{<span class="code-quote">"x.$"</span>:1}})
> t.find()
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4bde2ad3755d00000000710e"</span>), <span class="code-quote">"x"</span> : [ 1, 2, <span class="code-keyword">null</span>, 4, 3, 2, 3, 4 ] }
{ <span class="code-quote">"_id"</span> : ObjectId(<span class="code-quote">"4b9e4a1fc583fa1c76198319"</span>), <span class="code-quote">"x"</span> : [ 1, 3, 3, 2 ] }
四: Remove操作
这个操作在上一篇简单的说过,这里就不赘述了。