您好,登錄后才能下訂單哦!
更新
語法如下:
使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。