国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

淺談mongodb中query查詢

2020-03-14 13:21:49
字體:
供稿:網(wǎng)友

在MongoDB數(shù)據(jù)中,查詢有多種方法。比如find和or查詢等等。每種查詢方法也有不同的查詢條件供大家參考。

Mongodb最大的功能之一就是它支持動(dòng)態(tài)查詢,就跟傳統(tǒng)的關(guān)系型數(shù)據(jù)庫查詢一樣,但是它的查詢來的更靈活。

一、 Query Expression Objects:查詢表達(dá)式對(duì)象

查詢表達(dá)式文檔也是一個(gè)BSON結(jié)構(gòu)的文檔,例如,我們可以用下面的查詢語句來查詢集合中的所有記錄:

db.users.find({})

這里,表達(dá)式對(duì)象是一個(gè)空文檔,在查詢的時(shí)候去去匹配所有的記錄。再看:

復(fù)制代碼代碼如下:

db.users.find({'last_name': 'Smith'})

這里,我們將會(huì)查詢出所有“last_name”屬性值為“Smith”的文檔記錄。

二、查詢選項(xiàng)

除了查詢表達(dá)式意外,Mongodb還支持一些額外的參數(shù)選項(xiàng)。例如,我們可能僅僅只想返回某些特定的字段值:

復(fù)制代碼代碼如下:

//返回除了age字段外的所有字段

> db.user.find({},{age:0});

//返回tags=tennis 除了comments的所有列

db.posts.find( { tags : 'tennis' }, { comments : 0 } );

//返回userid=16的name字段

> db.user.find({userid:16},{name:1});

{ "_id" : 16, "name" : "user16" }

//返回x=john的所有z字段

db.things.find( { x : "john" }, { z : 1 } );

注: _id字段始終都會(huì)被返回,哪怕沒有明確指定

三、查詢條件

1) <, <=, >, >=

 

 
  1. // 大于: field > value  
  2. db.collection.find({ "field" : { $gt: value } } );  
  3.  
  4. //小于:field < value  
  5. db.collection.find({ "field" : { $lt: value } } ); 
  6.  
  7. //大于等于: field >= value  
  8. db.collection.find({ "field" : { $gte: value } } );  
  9.  
  10. //小于等于:field<=value  
  11. db.collection.find({ "field" : { $lte: value } } );  

2) $all

$all操作類似$in操作,但是不同的是,$all操作要求數(shù)組里面的值全部被包含在返回的記錄里面,如:

 

 
  1. > use test;  
  2. switched to db test 
  3.  
  4. > db.things.insert({a:[1,2,3]});  
  5.  
  6. > db.things.find();  
  7. "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }  
  8.  
  9. > db.things.find({a:{$all:[2,3]}});  
  10. "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }  
  11.  
  12. > db.things.find({a:{$all:[1,2,3]}});  
  13. "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }  
  14.  
  15. > db.things.find({a:{$all:[1]}});  
  16. "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }  
  17.  
  18. > db.things.find({a:{$all:[1,2,3,4]}}); 

3) $exists

$exists操作檢查一個(gè)字段是否存在,如:

 

 
  1. for(var i=0;i<1000;i++) db.user.save({_id:i,name:'user'+i,userid:i,age:20});  
  2.  
  3. //包含userid  
  4. > db.user.find({userid:{$exists:true}}).limit(5);  
  5. "_id" : 0, "name" : "user0""userid" : 0, "age" : 20 }  
  6. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  7. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  8. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  9. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  
  10.  
  11. //不包含sex字段  
  12. > db.user.find({sex:{$exists:false}}).limit(5); 

4) $mod

$mod操作可以讓我們簡單的進(jìn)行取模操作,而不需要用到where子句,如:

 

 
  1. //where子句  
  2. > db.user.find("this._id%10==1").limit(5);  
  3. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  4. "_id" : 11, "name" : "user11""userid" : 11, "age" : 20 }  
  5. "_id" : 21, "name" : "user21""userid" : 21, "age" : 20 }  
  6. "_id" : 31, "name" : "user31""userid" : 31, "age" : 20 }  
  7. "_id" : 41, "name" : "user41""userid" : 41, "age" : 20 }  
  8.  
  9. //$mod操作  
  10. > db.user.find({_id:{$mod:[10,1]}}).limit(5);  
  11. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  12. "_id" : 11, "name" : "user11""userid" : 11, "age" : 20 }  
  13. "_id" : 21, "name" : "user21""userid" : 21, "age" : 20 }  
  14. "_id" : 31, "name" : "user31""userid" : 31, "age" : 20 }  
  15. "_id" : 41, "name" : "user41""userid" : 41, "age" : 20 }  

5) $ne

$ne意思是not equal,不等于,不用多說,看例子:

 

 
  1. > db.user.find().limit(5);  
  2. "_id" : 0, "name" : "user0""userid" : 0, "age" : 20 }  
  3. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  4. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  5. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  6. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  
  7.  
  8. > db.user.find({_id:{$ne:0}}).limit(5);  
  9. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  10. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  11. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  12. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  
  13. "_id" : 5, "name" : "user5""userid" : 5, "age" : 20 } 

6) $in

$in操作類似于傳統(tǒng)關(guān)系數(shù)據(jù)庫中的IN,看例子:

 

 
  1. //數(shù)據(jù)庫中有所有數(shù)組對(duì)應(yīng)的記錄  
  2. > db.user.find({_id:{$in:[2,3,4,5,6]}}).limit(5);  
  3. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  4. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  5. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  
  6. "_id" : 5, "name" : "user5""userid" : 5, "age" : 20 }  
  7. "_id" : 6, "name" : "user6""userid" : 6, "age" : 20 }  
  8.  
  9. //因?yàn)閿?shù)據(jù)庫中沒有_id=1111的記錄  
  10. > db.user.find({_id:{$in:[2,3,4,5,1111]}}).limit(5);  
  11. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  12. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  13. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  
  14. "_id" : 5, "name" : "user5""userid" : 5, "age" : 20 } 

7) $nin

$nin跟$in操作相反,看例子:

 

 
  1. //扣掉_id=1/2/3/4的記錄  
  2. > db.user.find({_id:{$nin:[1,2,3,4]}}).limit(5);  
  3. "_id" : 0, "name" : "user0""userid" : 0, "age" : 20 }  
  4. "_id" : 5, "name" : "user5""userid" : 5, "age" : 20 }  
  5. "_id" : 6, "name" : "user6""userid" : 6, "age" : 20 }  
  6. "_id" : 7, "name" : "user7""userid" : 7, "age" : 20 }  
  7. "_id" : 8, "name" : "user8""userid" : 8, "age" : 20 }  

8) $nor、$or

$nor跟$or相反,不好解釋,看例子:

 

 
  1. > db.user.find({$nor:[{_id:2},{name:'user3'},{userid:4}]}).limit(5);  
  2. "_id" : 0, "name" : "user0""userid" : 0, "age" : 20 }  
  3. "_id" : 1, "name" : "user1""userid" : 1, "age" : 20 }  
  4. "_id" : 5, "name" : "user5""userid" : 5, "age" : 20 }  
  5. "_id" : 6, "name" : "user6""userid" : 6, "age" : 20 }  
  6. "_id" : 7, "name" : "user7""userid" : 7, "age" : 20 } 
  7.  
  8. > db.user.find({$or:[{_id:2},{name:'user3'},{userid:4}]}).limit(5);  
  9. "_id" : 2, "name" : "user2""userid" : 2, "age" : 20 }  
  10. "_id" : 3, "name" : "user3""userid" : 3, "age" : 20 }  
  11. "_id" : 4, "name" : "user4""userid" : 4, "age" : 20 }  

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赣州市| 临漳县| 大丰市| 宁河县| 阿勒泰市| 蒙城县| 榕江县| 无锡市| 聂荣县| 彩票| 汉沽区| SHOW| 吕梁市| 渭源县| 阳江市| 镇原县| 瓦房店市| 沿河| 广安市| 松原市| 鹰潭市| 玉林市| 忻州市| 贺州市| 大埔区| 龙井市| 察隅县| 七台河市| 蓝山县| 山东| 历史| 寻甸| 满城县| 津南区| 南投市| 西和县| 高淳县| 八宿县| 定陶县| 宁夏| 昆山市|