Mongodb最大的功能之一就是它支持動態(tài)查詢,就跟傳統(tǒng)的關(guān)系型數(shù)據(jù)庫查詢一樣,但是它的查詢來的更靈活。
一、 Query Expression Objects:查詢表達(dá)式對象
查詢表達(dá)式文檔也是一個BSON結(jié)構(gòu)的文檔,例如,我們可以用下面的查詢語句來查詢集合中的所有記錄:
db.users.find({})
這里,表達(dá)式對象是一個空文檔,在查詢的時候去去匹配所有的記錄。再看:
復(fù)制代碼 代碼如下:
db.users.find({'last_name': 'Smith'})
這里,我們將會查詢出所有“l(fā)ast_name”屬性值為“Smith”的文檔記錄。
二、查詢選項
除了查詢表達(dá)式意外,Mongodb還支持一些額外的參數(shù)選項。例如,我們可能僅僅只想返回某些特定的字段值:
復(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字段始終都會被返回,哪怕沒有明確指定
三、查詢條件
1) , =, >, >=
// 大于: field > value
db.collection.find({ "field" : { $gt: value } } );
//小于:field value
db.collection.find({ "field" : { $lt: value } } );
//大于等于: field >= value
db.collection.find({ "field" : { $gte: value } } );
//小于等于:field=value
db.collection.find({ "field" : { $lte: value } } );
2) $all
$all操作類似$in操作,但是不同的是,$all操作要求數(shù)組里面的值全部被包含在返回的記錄里面,如:
> use test;
switched to db test
> db.things.insert({a:[1,2,3]});
> db.things.find();
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find({a:{$all:[2,3]}});
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find({a:{$all:[1,2,3]}});
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find({a:{$all:[1]}});
{ "_id" : ObjectId("4de73360059e7f4bdf907cfe"), "a" : [ 1, 2, 3 ] }
> db.things.find({a:{$all:[1,2,3,4]}});
3) $exists
$exists操作檢查一個字段是否存在,如:
> for(var i=0;i1000;i++) db.user.save({_id:i,name:'user'+i,userid:i,age:20});
//包含userid
> db.user.find({userid:{$exists:true}}).limit(5);
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
//不包含sex字段
> db.user.find({sex:{$exists:false}}).limit(5);
4) $mod
$mod操作可以讓我們簡單的進(jìn)行取模操作,而不需要用到where子句,如:
//where子句
> db.user.find("this._id%10==1").limit(5);
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 11, "name" : "user11", "userid" : 11, "age" : 20 }
{ "_id" : 21, "name" : "user21", "userid" : 21, "age" : 20 }
{ "_id" : 31, "name" : "user31", "userid" : 31, "age" : 20 }
{ "_id" : 41, "name" : "user41", "userid" : 41, "age" : 20 }
//$mod操作
> db.user.find({_id:{$mod:[10,1]}}).limit(5);
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 11, "name" : "user11", "userid" : 11, "age" : 20 }
{ "_id" : 21, "name" : "user21", "userid" : 21, "age" : 20 }
{ "_id" : 31, "name" : "user31", "userid" : 31, "age" : 20 }
{ "_id" : 41, "name" : "user41", "userid" : 41, "age" : 20 }
5) $ne
$ne意思是not equal,不等于,不用多說,看例子:
> db.user.find().limit(5);
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
> db.user.find({_id:{$ne:0}}).limit(5);
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
6) $in
$in操作類似于傳統(tǒng)關(guān)系數(shù)據(jù)庫中的IN,看例子:
//數(shù)據(jù)庫中有所有數(shù)組對應(yīng)的記錄
> db.user.find({_id:{$in:[2,3,4,5,6]}}).limit(5);
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 }
//因為數(shù)據(jù)庫中沒有_id=1111的記錄
> db.user.find({_id:{$in:[2,3,4,5,1111]}}).limit(5);
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
7) $nin
$nin跟$in操作相反,看例子:
//扣掉_id=1/2/3/4的記錄
> db.user.find({_id:{$nin:[1,2,3,4]}}).limit(5);
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 }
{ "_id" : 7, "name" : "user7", "userid" : 7, "age" : 20 }
{ "_id" : 8, "name" : "user8", "userid" : 8, "age" : 20 }
8) $nor、$or
$nor跟$or相反,不好解釋,看例子:
> db.user.find({$nor:[{_id:2},{name:'user3'},{userid:4}]}).limit(5);
{ "_id" : 0, "name" : "user0", "userid" : 0, "age" : 20 }
{ "_id" : 1, "name" : "user1", "userid" : 1, "age" : 20 }
{ "_id" : 5, "name" : "user5", "userid" : 5, "age" : 20 }
{ "_id" : 6, "name" : "user6", "userid" : 6, "age" : 20 }
{ "_id" : 7, "name" : "user7", "userid" : 7, "age" : 20 }
> db.user.find({$or:[{_id:2},{name:'user3'},{userid:4}]}).limit(5);
{ "_id" : 2, "name" : "user2", "userid" : 2, "age" : 20 }
{ "_id" : 3, "name" : "user3", "userid" : 3, "age" : 20 }
{ "_id" : 4, "name" : "user4", "userid" : 4, "age" : 20 }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:- java操作mongodb基礎(chǔ)(查詢 排序 輸出list)
- MongoDB各種查詢操作詳解
- php操作MongoDB基礎(chǔ)教程(連接、新增、修改、刪除、查詢)
- java查詢mongodb中的objectid示例
- MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法
- MongoDB查詢操作限制返回字段的方法
- MongoDB查詢技巧總結(jié)
- Python中的MongoDB基本操作:連接、查詢實例
- MongoDB導(dǎo)出查詢結(jié)果到文件例子
- Java操作MongoDB模糊查詢和分頁查詢