更新
语法如下:
使用update方法来更新集合中的数据。update有四个参数,前两个参数是必须的。
db.person.update({"name”:”meteor”},{“$set":{"age":35}},true,true);
第一个参数:查询器,定位需要更新的目标文档(定义匹配条件)。
第二个参数:修改器文档,指定修改内容。
第三个参数:true表示要使用upsert,即如果没有找到符合更新条件的文档,就会以这个"条件和更新文档"为基础创建一个新的文档。如果找到了匹配的文档,则正常更新。default: false
第四个参数:true表示符合条件的所有文档,都要执行更新。default: false
修改器
常用操作符:$set、$inc、$push、$addToSet、$each、$pop、$pull、$unset
$set:用来指定一个字段的值。如果这个字段不存在,则创建它。对于更新而言,对符合更新条件的文档,修改执行的字段,不需要全部覆盖。
> db.emp.find() { "_id" : 1, "ename" : "tom", "age" : 25, "department" : "Sales", "salary" : 6000 } { "_id" : 2, "ename" : "eric", "age" : 24, "department" : "HR", "salary" : 4500 } { "_id" : 3, "ename" : "robin", "age" : 30, "department" : "Sales", "salary" : 8000 } { "_id" : 4, "ename" : "jack", "age" : 28, "department" : "Development", "salary" : 8000 } { "_id" : 5, "ename" : "Mark", "age" : 22, "department" : "Development", "salary" : 6500 } { "_id" : 6, "ename" : "marry", "age" : 23, "department" : "Planning", "salary" : 5000 } { "_id" : 7, "ename" : "hellen", "age" : 32, "department" : "HR", "salary" : 6000 } { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 7000 } > db.emp.find().forEach( ... function(item){ ... db.emp.update({"_id":item._id},{"$set": {"salary":item.salary+item.salary*.2}},false,true) ... }) > db.emp.find() { "_id" : 1, "ename" : "tom", "age" : 25, "department" : "Sales", "salary" : 7200 } { "_id" : 2, "ename" : "eric", "age" : 24, "department" : "HR", "salary" : 5400 } { "_id" : 3, "ename" : "robin", "age" : 30, "department" : "Sales", "salary" : 9600 } { "_id" : 4, "ename" : "jack", "age" : 28, "department" : "Development", "salary" : 9600 } { "_id" : 5, "ename" : "Mark", "age" : 22, "department" : "Development", "salary" : 7800 } { "_id" : 6, "ename" : "marry", "age" : 23, "department" : "Planning", "salary" : 6000 } { "_id" : 7, "ename" : "hellen", "age" : 32, "department" : "HR", "salary" : 7200 } { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400 } >
$inc:用来增加已有键的值,或者该键不存在就创建一个
> db.emp.update({},{$inc:{"age":2}},false,true) WriteResult({ "nMatched" : 8, "nUpserted" : 0, "nModified" : 8 }) > db.emp.find() { "_id" : 1, "ename" : "tom", "age" : 27, "department" : "Sales", "salary" : 7200 } { "_id" : 2, "ename" : "eric", "age" : 26, "department" : "HR", "salary" : 5400 } { "_id" : 3, "ename" : "robin", "age" : 32, "department" : "Sales", "salary" : 9600 } { "_id" : 4, "ename" : "jack", "age" : 30, "department" : "Development", "salary" : 9600 } { "_id" : 5, "ename" : "Mark", "age" : 24, "department" : "Development", "salary" : 7800 } { "_id" : 6, "ename" : "marry", "age" : 25, "department" : "Planning", "salary" : 6000 } { "_id" : 7, "ename" : "hellen", "age" : 34, "department" : "HR", "salary" : 7200 } { "_id" : 8, "ename" : "sarah", "age" : 26, "department" : "Development", "salary" : 8400 } > db.emp.update({},{$inc:{"age":-2}},false,true) WriteResult({ "nMatched" : 8, "nUpserted" : 0, "nModified" : 8 }) > db.emp.find() { "_id" : 1, "ename" : "tom", "age" : 25, "department" : "Sales", "salary" : 7200 } { "_id" : 2, "ename" : "eric", "age" : 24, "department" : "HR", "salary" : 5400 } { "_id" : 3, "ename" : "robin", "age" : 30, "department" : "Sales", "salary" : 9600 } { "_id" : 4, "ename" : "jack", "age" : 28, "department" : "Development", "salary" : 9600 } { "_id" : 5, "ename" : "Mark", "age" : 22, "department" : "Development", "salary" : 7800 } { "_id" : 6, "ename" : "marry", "age" : 23, "department" : "Planning", "salary" : 6000 } { "_id" : 7, "ename" : "hellen", "age" : 32, "department" : "HR", "salary" : 7200 } { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400 } >
$push:向已有数组末尾加入一个元素
> db.emp.update({"ename":"sarah"},{"$set":{"language":["Chinese"]}},true,true) //对符合name等于sarah的文档,添加一个language的数组 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese" ] } > db.emp.update({"ename":"sarah"},{"$push":{"language":"English"}},true,true) //给数组的末尾添加一个值 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese", "English" ] } >
$addToSet:避免向数组插入重复的值
> db.emp.update({"ename":"sarah"},{"$addToSet":{"language":"English"}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.emp.update({"ename":"sarah"},{"$addToSet":{"language":"Russian"}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese", "English", "Russian" ] } > db.emp.update({"ename":"sarah"},{"$addToSet":{"language":"Russian"}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese", "English", "Russian" ] } >
$each:与$push和$addToSet结合,一次给数组添加多个值
> db.emp.update({"ename":"sarah"},{"$addToSet":{"language":{"$each":["German","French"]}}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese", "English", "Russian", "German", "French" ] } >
$pop:可以从数组的任何一端删除元素
> db.emp.update({"ename":"sarah"},{"$pop":{"language":1}},true,true)//从数组的末尾删除一个元素 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "Chinese", "English", "Russian", "German" ] } > db.emp.update({"ename":"sarah"},{"$pop":{"language":-1}},true,true)//从数组的头部删除一个元素 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "English", "Russian", "German" ] } >
$pull:删除数组对应的值
> db.emp.update({"ename":"sarah"},{"$pull":{"language":"Russian"}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "English", "German" ] } > db.emp.update({"ename":"sarah"},{"$pull":{"language":"German"}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400, "language" : [ "English" ] }
$unset : 删除文档中指定的字段
> db.emp.update({"ename":"sarah"},{"$unset":{"language":1}},true,true) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.emp.find({"ename":"sarah"}) { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400 } >
符合条件的文档只更新第一条记录,如果没有更新字段,则添加:
db.col.update( { "age" : { $gt : 25 } } , { $set : { "test2" : "OK"} } );
符合条件的文档只更新第一条记录,如果没有更新字段,则忽略:
db.col.update( { "age" : { $gt : 25 } } , { $inc : { "age" : 1} },false,false);
符合条件的文档全部更新,如果没有更新字段,则添加:
db.col.update( { "age" : { $gt : 25 } } , { $set : { "test2" : "OK"} },false,true );
符合条件的文档只添加第一条,如果没有更新字段,则添加:
db.col.update( { "age" : { $gt : 25 } } , { $set : { "test2” : "OK"} },true,false );
符合条件的文档全部添加加进去(覆盖原始字段的值):
db.col.update( { "age" : { $gt : 25 } } , { $set : { "test2” : "OK"} },true,true );
删除
使用remove方法删除集合中的数据。它可以接受一个查询文档作为可选参数。给定这个参数以后,只有符合条件的文档才能被删除。(删除数据是永久性的,不能撤销,也不能恢复)。
db.emp.remove({"name”:”sarah”})//删除person集合中name字段的值等于ryan的所有文档。
db.emp.remove()//删除person集合中所有的文档。
使用drop方法代替remove方法,可以大幅度提高删除数据的速度。但是这个方法不能指定任何限定条件。而且整个集合都会被删除,包括索引等信息,甚用!!
db.emp.drop()
查询
MongoDB中使用find方法来进行查询。查询就是返回一个集合中文档的子集,子集的范围从0个文档到整个集合。
find方法接受两个参数:
第一个参数决定了要返回哪些文档,参数的内容是查询的条件。
第二个参数来指定想要的键(字段)。第二个参数存在的情况:键的值为1代表要显示,为0代表不显示。“_id”默认显示,其他默认不显示。第二个参数不存在的情况:所有字段默认显示。
> db.emp.find({"ename":"sarah"},{"ename":1})
{ "_id" : 8, "ename" : "sarah" }
>
常见操作符: $lt、$lte、$gt、$gte、$ne、$in、$nin、$or、$exists、$not、$mod、$size、正则表达式
查询条件:
$lt、$lte、$gt、$gte $ne 比较操作符(没有$eq这个操作符),分别对应<、<=、>、>=、!=。
> db.emp.find({"age":{"$lt":25}},{"ename":1}) { "_id" : 2, "ename" : "eric" } { "_id" : 5, "ename" : "Mark" } { "_id" : 6, "ename" : "marry" } { "_id" : 8, "ename" : "sarah" } > db.emp.find({"age":{"$gt":25}},{"ename":1}) { "_id" : 3, "ename" : "robin" } { "_id" : 4, "ename" : "jack" } { "_id" : 7, "ename" : "hellen" } > db.emp.find({"age":25},{"ename":1}) { "_id" : 1, "ename" : "tom" } >
$in、$nin,用来查询一个键的多个值。
> db.emp.find({"age":{"$in":[22,25,28]}},{"ename":1}) { "_id" : 1, "ename" : "tom" } { "_id" : 4, "ename" : "jack" } { "_id" : 5, "ename" : "Mark" } > db.emp.find({"age":{"$nin":[22,25,28]}},{"ename":1}) { "_id" : 2, "ename" : "eric" } { "_id" : 3, "ename" : "robin" } { "_id" : 6, "ename" : "marry" } { "_id" : 7, "ename" : "hellen" } { "_id" : 8, "ename" : "sarah" } >
$or,用来查询多个键的多个值(可以和$in等配合使用)
> db.emp.find({"$or":[{"age":25},{"salary":7800}]},{"ename":1,_id:0,age:1,salary:1}) { "ename" : "tom", "age" : 25, "salary" : 7200 } { "ename" : "Mark", "age" : 22, "salary" : 7800 } >
$exists,查询的键对应是值是null的,默认会返回null和键不存在的文档。可以通过$exists来判断该键是否存在。
> db.emp.find({"language":{"$exists":false}}) { "_id" : 1, "ename" : "tom", "age" : 25, "department" : "Sales", "salary" : 7200 } { "_id" : 2, "ename" : "eric", "age" : 24, "department" : "HR", "salary" : 5400 } { "_id" : 3, "ename" : "robin", "age" : 30, "department" : "Sales", "salary" : 9600 } { "_id" : 4, "ename" : "jack", "age" : 28, "department" : "Development", "salary" : 9600 } { "_id" : 5, "ename" : "Mark", "age" : 22, "department" : "Development", "salary" : 7800 } { "_id" : 6, "ename" : "marry", "age" : 23, "department" : "Planning", "salary" : 6000 } { "_id" : 7, "ename" : "hellen", "age" : 32, "department" : "HR", "salary" : 7200 } { "_id" : 8, "ename" : "sarah", "age" : 24, "department" : "Development", "salary" : 8400 } > db.emp.find({"language":{"$exists":true}}) >
$mod , 取模。例查找age字段模10余2的记录
> db.user.find({"age":{"$mod":[10,2]}}) { "_id" : 6, "ename" : "marry", "age" : 22, "department" : "Planning", "salary" : 5000 } >
$size, 返回指定大小数组元素的记录
> db.user.find({"language":{"$exists":true}}) { "_id" : 4, "ename" : "jack", "age" : 27, "department" : "Development", "salary" : 8300, "language" : [ "Chinese", "English", "Russian", "German", "Japanese" ] } > db.user.find({"language":{"$size":5}}) { "_id" : 4, "ename" : "jack", "age" : 27, "department" : "Development", "salary" : 8300, "language" : [ "Chinese", "English", "Russian", "German", "Japanese" ] }
$all , db.collection.find({"KeyName":{"$all":[value1,value2]}}) 判断KeyName中是否完全包含 value1 和 value2
> db.user.find({"language":{"$all":["English","Latin"]}}) > db.user.find({"language":{"$in":["English","Latin"]}}) { "_id" : 4, "ename" : "jack", "age" : 27, "department" : "Development", "salary" : 8300, "language" : [ "Chinese", "English", "Russian", "German", "Japanese" ] }
正则表达式:
> db.user.find({"ename":/.ark/i}) { "_id" : 5, "ename" : "Mark", "age" : 21, "department" : "Development", "salary" : 6500 } > db.user.find({"ename":/.a.k/i}) { "_id" : 4, "ename" : "jack", "age" : 27, "department" : "Development", "salary" : 8300, "language" : [ "Chinese", "English", "Russian", "German", "Japanese" ] } { "_id" : 5, "ename" : "Mark", "age" : 21, "department" : "Development", "salary" : 6500 } >
$where,用它可以在查询中执行任意的javascript,这样就能在查询中做(几乎)任何事情。为了安全起见,应该严格限制或者消除"$where"语句的使用。
db.person.find({"$where":function(){
...;//这里可以是任意的javascript语句。
}})
游标, 利用游标可以限制结果的数量,略过部分结果,根据任意键按任意顺序的组合对结果进行各种排序,或者是执行其他的一些强大的操作。
var cursor = db.person.find();
while(cursor.hasNext()){
obj = cursor.next();
...;//这里可以做任何事情
}
常用的shell:
limit:只返回前面多少个结果。
db.person.find().limit(2)
skip:跳过多少个结果后显示剩余的。
db.person.find().skip(2)
sort:用于排序。接受一个对象(一组键值对)作为参数,键对应文档的键名,值代表排序的方向。排序的方向可以是1(升序)或者-1(降序)。如果指定了多个键,则按照这些键被指定的顺序逐个排序。
db.person.find().sort({"name":1,"age":-1})//查询的结果,按照name升序,age降序来排序显示
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。