MongoDB常用操作一查詢find方法db.collection_name.find()


來:http://blog.csdn.net/wangli61289/article/details/40623097

https://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

【第一個查詢參數】

find函數第一個參數是一個文檔,其中給出了我們要查詢集合中什么樣文檔的描述。如果我們要查詢所有文檔,可以不帶任何參數調用find函數,或第一個參數為空文檔{},如下例:

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. > db.people.find({});  
  6. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  7. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  8. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  9. >  


對於第一個代表查詢參數的文檔,我們該如何理解呢?比如我們執行 db.people.find({"age":18}),這個函數實際表示我們要查詢鍵"age"的值為18的文檔,鍵“age”的值不等於18或不存在這個鍵的文檔均不會被查詢到。再比如我們執行這個函數:db.people.find({"name":"jimmy", "age":18}),這個實際表示得是查詢鍵“name”的值為"jimmy"並且鍵"age"的值為18的文檔,即查詢文檔中各個條件是AND的關系!

在使用第一個參數時,我們需要注意一點:該查詢文檔中“鍵值對”中的值必須為常量!

【第二個查詢參數】

上例中,我們不指定或只指定find函數第一個參數查詢得到的文檔,會包含原集合文檔所有的鍵值對。這種情況對於某些鍵值對特別多的文檔並不適用,因為我們可能只關心該文檔的某幾個鍵值對。這時我們可以使用find函數的第二個參數,來指定返回的鍵值對,這樣還可以減少傳輸的數據量從而加快效率。第二個參數同樣是個文檔,如下例:

[javascript]  view plain copy
 
  1. > db.people.find({},{"age":1,"name":1});  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. > db.people.find({},{"age":1});  
  6. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18 }  
  7. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28 }  
  8. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38 }  
  9. > db.people.find({},{"name":1});  
  10. "_id" : ObjectId("501dffc605f64f64b0765c53"), "name" : "tom" }  
  11. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "name" : "jimmy" }  
  12. "_id" : ObjectId("501dffd005f64f64b0765c55"), "name" : "tim" }  
  13. > db.people.find({},{"name":1,"_id":0});  
  14. "name" : "tom" }  
  15. "name" : "jimmy" }  
  16. "name" : "tim" }  
  17. >  

使用find函數第二個參數,對於這個文檔我們有這些要注意的:

1》 對於非"_id"的所有鍵,其值要么同時不等於0(表明要查詢該鍵值對),要么同時等於0(表明要忽略該鍵值對),否則執行報錯。

2》 對於非“_id”的所有鍵,如果其值全部為0,則此時如果也指明了鍵“_id”的情況,則鍵“_id”的值必須為0,否則執行報錯。

3》 對於鍵“_id”,如果不指名其值,則查詢結果中肯定包含這個鍵值對,如果不想包含,可以指明其值為0即可。

綜上,我們總結一下如果需要使用第二個參數,則使用的技巧為:指明所有需要得到的鍵的值為非0的數字,如果還要過濾鍵"_id",則再指明"_id"這個鍵值為0即可。

【查詢條件】

上面提到的查詢條件,都是精確匹配,即“=”多少。MongoDB中的查詢顯然還有更復雜的匹配。比如范圍,OR子句和取反等。我們分別進行介紹。

“$lt”、“$lte”、“$gt”、“$gte” 就是所有的范圍比較操作符,分別對應<、<=、>、>=。將他們組合應用,就可以查詢值在某個范圍內的文檔了,比如我們要查詢18-30歲(含)的所有用戶:

[javascript]  view plain copy
 
  1. > db.people.find({"age":{"$gte":18,"$lte":30}});  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. >  

這種范圍查詢操作符,除用在值為數字的鍵之上外,對於值為日期的鍵的范圍匹配也尤為好用!比如我們要查詢注冊日期在2007/05/01前的用戶信息,我們可以這樣寫:

[javascript]  view plain copy
 
  1. > db.user.find();  
  2. "_id" : ObjectId("5020faf5d6acd1b2a3fb316f"), "name" : "tim", "age" : 40, "reg  
  3. istered" : ISODate("2007-03-02T16:00:00Z") }  
  4. "_id" : ObjectId("5020fb08d6acd1b2a3fb3170"), "name" : "tom", "age" : 29, "reg  
  5. istered" : ISODate("2009-07-02T16:00:00Z") }  
  6. "_id" : ObjectId("5020fb27d6acd1b2a3fb3171"), "name" : "jimmy", "age" : 18, "r  
  7. egistered" : ISODate("2009-09-02T16:00:00Z") }  
  8. > db.user.find({"registered":{"$lte":new Date("2007/05/01")}});  
  9. "_id" : ObjectId("5020faf5d6acd1b2a3fb316f"), "name" : "tim", "age" : 40, "reg  
  10. istered" : ISODate("2007-03-02T16:00:00Z") }  
  11. >  

有時我們需要查詢某個鍵不等於某個值的文檔,我們可以使用這個條件操作符"$ne",他表示不相等。如我們要查詢注冊用戶名不等於“tom”的文檔:

[javascript]  view plain copy
 
  1. > db.user.find({"name":{"$ne":"tom"}});  
  2. "_id" : ObjectId("5020faf5d6acd1b2a3fb316f"), "name" : "tim", "age" : 40, "reg  
  3. istered" : ISODate("2007-03-02T16:00:00Z") }  
  4. "_id" : ObjectId("5020fb27d6acd1b2a3fb3171"), "name" : "jimmy", "age" : 18, "r  
  5. egistered" : ISODate("2009-09-02T16:00:00Z") }  
  6. >  

條件操作符"$ne"適合於所有類型的值!

【OR查詢】

MongoDB中有兩種方式進行OR查詢:“$in”可以用來查詢一個鍵的多個值,“$or”則更通用一些,可以用來完成多個鍵值對的組合。我們也分別演示一下:我們要查詢獎券號碼為10,20,30 的所有投注者的姓名:

[javascript]  view plain copy
 
  1. > db.raffle.find({})  
  2. "_id" : ObjectId("50210091d6acd1b2a3fb3172"), "name" : "tim", "ticket_no" : 11 }  
  3. "_id" : ObjectId("5021009bd6acd1b2a3fb3173"), "name" : "tom", "ticket_no" : 20 }  
  4. "_id" : ObjectId("502100a8d6acd1b2a3fb3174"), "name" : "jimmy", "ticket_no" : 30 }  
  5. > db.raffle.find({"ticket_no":{"$in":[10,20,30]}}, {"name":1})  
  6. "_id" : ObjectId("5021009bd6acd1b2a3fb3173"), "name" : "tom" }  
  7. "_id" : ObjectId("502100a8d6acd1b2a3fb3174"), "name" : "jimmy" }  
  8. >  

但如果還要求我們查詢出獎券號碼為10,20,30 或投注者姓名為“tim”的所有投注信息,我們單純用"$in"是無法勝任的,我們可以用操作符"$or",“$or”操作符可以組合其他操作符如"$in"拼湊的條件:

[javascript]  view plain copy
 
  1. > db.raffle.find({})  
  2. "_id" : ObjectId("50210091d6acd1b2a3fb3172"), "name" : "tim", "ticket_no" : 11 }  
  3. "_id" : ObjectId("5021009bd6acd1b2a3fb3173"), "name" : "tom", "ticket_no" : 20 }  
  4. "_id" : ObjectId("502100a8d6acd1b2a3fb3174"), "name" : "jimmy", "ticket_no" : 30 }  
  5. > db.raffle.find({"$or":[{"ticket_no":{"$in":[10,20,30]}},{"name":"tim"}]});  
  6. "_id" : ObjectId("50210091d6acd1b2a3fb3172"), "name" : "tim", "ticket_no" : 11 }  
  7. "_id" : ObjectId("5021009bd6acd1b2a3fb3173"), "name" : "tom", "ticket_no" : 20 }  
  8. "_id" : ObjectId("502100a8d6acd1b2a3fb3174"), "name" : "jimmy", "ticket_no" : 30 }  
  9. >  

使用“$or”操作符,其值為一個條件數組,數組中各個條件最后通過or組合。使用這個條件操作符有一個最佳實踐是:將最寬松的條件放在前面,這樣可以加快文檔匹配速度!

【$mod 和 $not】

$mod操作符,使用格式為 {"鍵":{"$mod":[num1, num2]}},查詢“鍵”的值對num1取余,如果這個值等於num2,則整條文檔符合條件。如我們要查詢所有在其本命年的用戶(年齡是12的整數倍):

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  6. > db.people.find({"age":{"$mod":[12,0]}});  
  7. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  8. >  

$not是元條件符,即可以用於任何其他條件之上的,表明取反,還是上面的例子,我們這次要查所有不在其本命年的用戶文檔信息:

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  6. > db.people.find({"age":{"$not":{"$mod":[12,0]}}});  
  7. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  8. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  9. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  10. >  

$not條件符配合正則表達式使用非常強大,這里先預報一下,我們后續會提到引入正則表達式的查詢。

【特定於類型的查詢】

查詢某鍵,其值為null的文檔時,我們會發現一個奇怪的現象,我們先看一個例子:

[javascript]  view plain copy
 
  1. > db.cc.find();  
  2. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  3. "_id" : ObjectId("50210867d6acd1b2a3fb3177"), "x" : 0, "y" : 1 }  
  4. "_id" : ObjectId("5021086ad6acd1b2a3fb3178"), "x" : 0, "y" : 2 }  
  5. > db.cc.find({"y":null});  
  6. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  7. >  

利用值為null的條件貌似可以查詢正確的文檔,但我們忽略了如果集合中存在沒有鍵"y"的文檔,會出現什么情況:

[javascript]  view plain copy
 
  1. > db.cc.find();  
  2. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  3. "_id" : ObjectId("50210867d6acd1b2a3fb3177"), "x" : 0, "y" : 1 }  
  4. "_id" : ObjectId("5021086ad6acd1b2a3fb3178"), "x" : 0, "y" : 2 }  
  5. "_id" : ObjectId("5021097ed6acd1b2a3fb3179"), "x" : 0 }  
  6. > db.cc.find({"y":null});  
  7. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  8. "_id" : ObjectId("5021097ed6acd1b2a3fb3179"), "x" : 0 }  
  9. >  

果然不出意外,沒有這個鍵的文檔同樣匹配值為null這種條件,如果我們需要過濾掉這種文檔,需要另外一個條件操作符$exists,指明這個鍵必須存在:

[javascript]  view plain copy
 
  1. > db.cc.find();  
  2. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  3. "_id" : ObjectId("50210867d6acd1b2a3fb3177"), "x" : 0, "y" : 1 }  
  4. "_id" : ObjectId("5021086ad6acd1b2a3fb3178"), "x" : 0, "y" : 2 }  
  5. "_id" : ObjectId("5021097ed6acd1b2a3fb3179"), "x" : 0 }  
  6. > db.cc.find({"y":{"$in":[null],$exists:true}});  
  7. "_id" : ObjectId("50210861d6acd1b2a3fb3176"), "x" : 0, "y" : null }  
  8. >  

我們發現,因為MongoDB中沒有提供類似於"$eq"這種相等的條件操作符,所以“=null”的判斷只能通過{"$in":[null]}來實現!

【正則表達式】

正則表達式在任何語言中都是操作字符串的一大利器!在MongoDB的查詢中,其依然威力不減。正則表達式可以靈活的匹配字符串類型的值。如我們要查詢所有姓名為“joy”開頭並且忽略大小寫的用戶文檔:

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  6. "_id" : ObjectId("50210c32d6acd1b2a3fb317a"), "age" : 19, "name" : "Joe" }  
  7. "_id" : ObjectId("50210c3cd6acd1b2a3fb317b"), "age" : 29, "name" : "JoE" }  
  8. "_id" : ObjectId("50210c40d6acd1b2a3fb317c"), "age" : 29, "name" : "JoEy" }  
  9. > db.people.find({"name":/joe.*/i});  
  10. "_id" : ObjectId("50210c32d6acd1b2a3fb317a"), "age" : 19, "name" : "Joe" }  
  11. "_id" : ObjectId("50210c3cd6acd1b2a3fb317b"), "age" : 29, "name" : "JoE" }  
  12. "_id" : ObjectId("50210c40d6acd1b2a3fb317c"), "age" : 29, "name" : "JoEy" }  
  13. >  

Shell中寫正則表達式的方式和JavaScript的一致,寫在一對“ // ”之間的就是正則表達式。具體正則表達式的一些寫法大家可以參考正則表達式的相關規范。我們上面提到了操作符$not和正則表達式的聯合使用,我們這邊也演示一下:

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  6. "_id" : ObjectId("50210c32d6acd1b2a3fb317a"), "age" : 19, "name" : "Joe" }  
  7. "_id" : ObjectId("50210c3cd6acd1b2a3fb317b"), "age" : 29, "name" : "JoE" }  
  8. "_id" : ObjectId("50210c40d6acd1b2a3fb317c"), "age" : 29, "name" : "JoEy" }  
  9. > db.people.find({"name":{"$not":/joe.*/i}});  
  10. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  11. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  12. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  13. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  14. >  

我們可以看到使用$not是將其作為正則表達式的鍵,表明和這個正則表達式不匹配。我們在最前邊也介紹了,MongoDB支持正則表達式這種數據類型,即“鍵值對”中允許“值”為正則表達式,對於這種鍵值對,正則表達式也可以匹配成功:

[javascript]  view plain copy
 
  1. > db.people.find();  
  2. "_id" : ObjectId("501dffc605f64f64b0765c53"), "age" : 18, "name" : "tom" }  
  3. "_id" : ObjectId("501dffcc05f64f64b0765c54"), "age" : 28, "name" : "jimmy" }  
  4. "_id" : ObjectId("501dffd005f64f64b0765c55"), "age" : 38, "name" : "tim" }  
  5. "_id" : ObjectId("50210579d6acd1b2a3fb3175"), "age" : 36, "name" : "cliff" }  
  6. "_id" : ObjectId("50210c32d6acd1b2a3fb317a"), "age" : 19, "name" : "Joe" }  
  7. "_id" : ObjectId("50210c3cd6acd1b2a3fb317b"), "age" : 29, "name" : "JoE" }  
  8. "_id" : ObjectId("50210c40d6acd1b2a3fb317c"), "age" : 29, "name" : "JoEy" }  
  9. "_id" : ObjectId("50210f63d6acd1b2a3fb317d"), "age" : 99, "name" : /joe/i }  
  10. > db.people.find({"name":/joe/i});  
  11. "_id" : ObjectId("50210c32d6acd1b2a3fb317a"), "age" : 19, "name" : "Joe" }  
  12. "_id" : ObjectId("50210c3cd6acd1b2a3fb317b"), "age" : 29, "name" : "JoE" }  
  13. "_id" : ObjectId("50210c40d6acd1b2a3fb317c"), "age" : 29, "name" : "JoEy" }  
  14. "_id" : ObjectId("50210f63d6acd1b2a3fb317d"), "age" : 99, "name" : /joe/i }  
  15. >  

但注意正則表達式的匹配必須是完全匹配,即正則表達式的寫法完全相同才可以匹配成功(這個在實際應用中基本不會遇見的)。

MongoDB可以為前綴型的正則表達式(/^joe/i)查詢使用索引,所以這種前綴型的正則表式查詢速度會很快!

 

這里再稍微提一下find函數和findOne函數的區別,剛才說了,find函數返回的是一個子集,而findOne就是返回一條文檔或null(未查詢到結果),對於一些可以接受文檔做參數的函數如insert,可以直接使用findOne的返回值作為參數。

 

最后就這基本的查詢條件操作符部分稍作總結,因為我們前面介紹修改器操作符也是以"$"開頭,如“$set”,"$inc"等,這里介紹的查詢條件操作符也是以"$"開頭,那使用上有哪些區分呢?修改器操作符都是外層文檔的鍵,而查詢條件的操作符基本都是內層文檔的鍵(注意“$or”是一個例外)。

select * from users;

db.users.find();

指定返回那些列(鍵):

select name, skills from users;

db.users.find({}, {'name' : 1, 'skills' : 1});

補充說明: 第一個{} 放where條件 第二個{} 指定那些列顯示和不顯示 (0表示不顯示 1表示顯示)

where條件:

1.簡單的等於:

select name, age, skills from users where name = 'hurry';

db.users.find({'name' : 'hurry'},{'name' : 1, 'age' : 1, 'skills' : 1});

2.使用and

select name, age, skills from users where name = 'hurry' and age = 18;

db.users.find({'name' : 'hurry', 'age' : 18},{'name' : 1, 'age' : 1, 'skills' : 1});

3.使用or

select name, age, skills from users where name = 'hurry' or age = 18;

db.users.find({ '$or' : [{'name' : 'hurry'}, {'age' : 18}] },{'name' : 1, 'age' : 1, 'skills' : 1});

4.<, <=, >, >= ($lt, $lte, $gt, $gte )

select * from users where age >= 20 and age <= 30;

db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});

5.使用in, not in ($in, $nin)

select * from users where age in (10, 22, 26);

db.users.find({'age' : {'$in' : [10, 22, 26]}});

6.匹配null

select * from users where age is null;

db.users.find({'age' : null);

7.like (mongoDB 支持正則表達式)

select * from users where name like "%hurry%";

db.users.find({name:/hurry/}); 

select * from users where name like "hurry%";

db.users.find({name:/^hurry/}); 

8.使用distinct

select distinct (name) from users;

db.users.distinct('name');

9.使用count

select count(*) from users;

dunt();



10.數組查詢 (mongoDB自己特有的)

如果skills是 ['java','python']

db.users.find({'skills' : 'java'}); 該語句可以匹配成功

$all

db.users.find({'skills' : {'$all' : ['java','python']}}) skills中必須同時包含java 和 python 

$size

db.users.find({'skills' : {'$size' : 2}}) 遺憾的是$size不能與$lt等組合使用

$slice

db.users.find({'skills' : {'$slice : [1,1]}})

兩個參數分別是偏移量和返回的數量

11.查詢內嵌文檔 


12.強大的$where查詢
db.foo.find();                   
{ "_id" : ObjectId("4e17ce0ac39f1afe0ba78ce4"), "a" : 1, "b" : 3, "c" : 10 }
{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }

如果要查詢 b = c 的文檔怎么辦?

> db.foo.find({"$where":function(){
    for(var current in this){
        for(var other in this){
            if(current != other && this[current] == this[other]){
                return true;    
            }
        }
    }
    return false;

}});



{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }



1 ) . 大於,小於,大於或等於,小於或等於
$gt:大於
$lt:小於
$gte:大於或等於
$lte:小於或等於
例子:
db.collection.find({ "field" : { $gt: value } } ); // greater than : field > value
db.collection.find({ "field" : { $lt: value } } ); // less than : field < value
db.collection.find({ "field" : { $gte: value } } ); // greater than or equal to : field >= value
db.collection.find({ "field" : { $lte: value } } ); // less than or equal to : field <= value



如查詢j大於3,小於4:


db.things.find({j : {$lt: 3}});
db.things.find({j : {$gte: 4}});



也可以合並在一條語句內:


db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value





2) 不等於 $ne

例子:


db.things.find( { x : { $ne : 3 } } );



3) in 和 not in ($in $nin)
語法:
db.collection.find( { "field" : { $in : array } } );



例子:


db.things.find({j:{$in: [2,4,6]}});


db.things.find({j:{$nin: [2,4,6]}});


4) 取模運算$mod
如下面的運算:
db.things.find( "this.a % 10 == 1")



可用$mod代替:


db.things.find( { a : { $mod : [ 10 , 1 ] } } )


5)  $all
$all和$in類似,但是他需要匹配條件內所有的值:
如有一個對象:
{ a: [ 1, 2, 3 ] }



下面這個條件是可以匹配的:


db.things.find( { a: { $all: [ 2, 3 ] } } );



但是下面這個條件就不行了:


db.things.find( { a: { $all: [ 2, 3, 4 ] } } );


6)  $size
$size是匹配數組內的元素數量的,如有一個對象:{a:["foo"]},他只有一個元素:
下面的語句就可以匹配:
db.things.find( { a : { $size: 1 } } );



官網上說不能用來匹配一個范圍內的元素,如果想找$size<5之類的,他們建議創建一個字段來保存元素的數量。

You cannot use $size to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size field that you increment when you add elements.


7)$exists

$exists用來判斷一個元素是否存在:

如:


db.things.find( { a : { $exists : true } } ); // 如果存在元素a,就返回
db.things.find( { a : { $exists : false } } ); // 如果不存在元素a,就返回


8)  $type

$type 基於 bson type來匹配一個元素的類型,像是按照類型ID來匹配,不過我沒找到bson類型和id對照表。


db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int


9)正則表達式
mongo支持正則表達式,如:
db.customers.find( { name : /acme.*corp/i } ); // 后面的i的意思是區分大小寫


10)  查詢數據內的值
下面的查詢是查詢colors內red的記錄,如果colors元素是一個數據,數據庫將遍歷這個數組的元素來查詢。
db.things.find( { colors : "red" } );


11) $elemMatch
如果對象有一個元素是數組,那么$elemMatch可以匹配內數組內的元素:
> t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } ) 
{ "_id" : ObjectId("4b5783300334000000000aa9"), 
"x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]
}

$elemMatch : { a : 1, b : { $gt : 1 } } 所有的條件都要匹配上才行。

注意,上面的語句和下面是不一樣的。


> t.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )

$elemMatch是匹配{ "a" : 1, "b" : 3 },而后面一句是匹配{ "b" : 99 }, { "a" : 11 } 

12)  查詢嵌入對象的值
db.postings.find( { "author.name" : "joe" } );



注意用法是author.name,用一個點就行了。更詳細的可以看這個鏈接: dot notation

舉個例子:


> db.blog.save({ title : "My First Post", author: {name : "Jane", id : 1}})



如果我們要查詢 authors name 是Jane的, 我們可以這樣:


> db.blog.findOne({"author.name" : "Jane"})



如果不用點,那就需要用下面這句才能匹配:


db.blog.findOne({"author" : {"name" : "Jane", "id" : 1}})



下面這句:


db.blog.findOne({"author" : {"name" : "Jane"}})



是不能匹配的,因為mongodb對於子對象,他是精確匹配。


13) 元操作符 $not 取反

如:


db.customers.find( { name : { $not : /acme.*corp/i } } );


db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

mongodb還有很多函數可以用,如排序,統計等,請參考原文。

mongodb目前沒有或(or)操作符,只能用變通的辦法代替,可以參考下面的鏈接:

+operations+in+query+expressions
分類: MongoDB



二、更新
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


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 ] }






============  數組元素操作示例 ================


> db.arraytest.insert({id:2, name:'leon', comments:[{id:'011', content:'cmt11'}, {id:'012', content:'cmt12'}, {id:'013', content:'cmt13'}]})



1. 數組內的元素可以直接查詢

> db.arraytest.find({'comments.id':'002'})

2. 更新數組中的某個節點的值,用$符號

db.arraytest.update({'comments.id':'012'}, {$set: {'comments.$.content':'cmtttt012'}})

3. 刪除數組中的某一列,變成null

> db.arraytest.update({'comments.id':'012'}, {$unset: {'comments.$':1}})

4. 向數組中添加一個元素,如果之前沒有元素則會新建數組

> db.arraytest.update({'comments.id':'112'}, {$push: {'comments.$.reply': {'rid':'r21', content:'reply22'}}}) 
============  數組元素操作示例 ================


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM