您好,登錄后才能下訂單哦!
在前面的文章里面主要介紹了MongoDB的文檔,集合,數據庫等操作和對文檔的增、刪、改相關知識,接下來會總結一點有關查詢的相關知識。
在MySQL中,我們知道數據查詢是優化的主要內容,讀寫分離等技術都是可以用來處理數據庫查詢優化的,足以見數據庫查詢是每個系統中很重要的一部分,之前介紹了find的簡單使用,下面會介紹一些相對比較復雜一點的查詢。
一、數據查詢
MySQL數據庫中主要是用select 結合where子句實現數據的查詢,功能特別強大,例如多表聯合查詢、支持正則表達式等。不在這里做過多的相關介紹。這里主要介紹MongoDB的相關查詢,MongoDB中主要用find()實現數據的查詢,同時也可以使用一些條件限制。
1.1顯示單條數據
在上篇文章中提到了find()的使用,但是每次查詢數據,都是查詢所有的,顯示其中的一部分,可以用it迭代。有時候我們想要查詢其中的一條數據,具體操作要根據具體需求實現。
MongoDB 查詢數據的語法
db.collection.find(query, projection)
query :可選,使用查詢操作符指定查詢條件
projection :可選,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數即可(默認省略)。可以使用 pretty() 方法以易讀的方式來讀取數據,,語法格式如下
>db.col.find().pretty()
pretty() 方法以格式化的方式來顯示所有文檔。
例如:
db.winner.find().pretty() { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 }
1、查詢某個集合中的所有數據
db.winner.find() { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec8"), "winne" : 45 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec9"), "winne" : 46 } { "_id" : ObjectId("592e7e14aaa464fa8a557eca"), "winne" : 47 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecb"), "winne" : 48 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecc"), "winne" : 49 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecd"), "winne" : 50 } { "_id" : ObjectId("592e7e14aaa464fa8a557ece"), "winne" : 51 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecf"), "winne" : 52 } { "_id" : ObjectId("592e7e14aaa464fa8a557ed0"), "winne" : 53 } { "_id" : ObjectId("592e7e14aaa464fa8a557ed1"), "winne" : 54 } Type "it" for more
默認顯示20條數據,其他數據可以輸入it迭代。
2、顯示一條數據
find()是輸出所有結果,里面可能有些文檔內容相同,但是“_id”肯定是不一樣的,這時我們可以使用findOne()方法查詢,或者可以使用db.winner.find({winne:1955}).limit(1)。
db.winner.find({winne:1955}) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } { "_id" : ObjectId("592e7d1eaaa464fa8a557e96"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e97"), "winne" : 1955 } { "_id" : ObjectId("592e7d1faaa464fa8a557e98"), "winne" : 1955 } { "_id" : ObjectId("592e7d21aaa464fa8a557e99"), "winne" : 1955 } { "_id" : ObjectId("592e7d22aaa464fa8a557e9a"), "winne" : 1955 } 假如要查詢winner集合中winne=1955的一條數據,而用find()查詢出所有的數據,這時就可以使用findOne() db.winner.findOne({winne:1955}) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 或者可以使用 db.winner.find({winne:1955}).limit(1) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 兩者的區別 findOne()有點類似MySQL里面的distinct,會返回查詢的第一條結果,如果搜索不到想要的數據就會 返回NULL, db.winner.findOne({winne:200888}) null db.winner.find({winne:1955}).limit(1)方法就和MySQL里面的limit是一樣的,主要是限制查詢結果的條數。
3、查詢滿足一定條件的數據
在MySQL中查詢時,可以結合where以及字段等信息查詢數據,而MongoDB中也是可以的,同樣可以支持一些條件判斷語句。
格式 | 范例 | RDBMS中的類似語句 | |
---|---|---|---|
等于 | {<key>:<value> } | db.col.find({"winne":"1995"}).pretty() | where |
小于 | {<key>:{$lt:<value>}} | db.col.find({" | where |
小于或等于 | {<key>:{$lte:<value>}} | db.col.find({" | where |
大于 | {<key>:{$gt:<value>}} | db.col.find({" | where |
大于或等于 | {<key>:{$gte:<value>}} | db.col.find({" | where |
不等于 | {<key>:{$ne:<value>}} | db.col.find({" | where |
$gt -------- greater than $gte --------- gt equal $lt -------- less than $lte --------- lt equal $ne ----------- not equal 1、查詢winner集合中winne<50的相關數據 db.winner.find({winne:{$lt :50}}) { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec8"), "winne" : 45 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec9"), "winne" : 46 } { "_id" : ObjectId("592e7e14aaa464fa8a557eca"), "winne" : 47 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecb"), "winne" : 48 } { "_id" : ObjectId("592e7e14aaa464fa8a557ecc"), "winne" : 49 } { "_id" : ObjectId("592e7e17aaa464fa8a557f28"), "winne" : 41 } { "_id" : ObjectId("592e7e17aaa464fa8a557f29"), "winne" : 42 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2a"), "winne" : 43 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2b"), "winne" : 44 } 2、查詢winner集合中40=<winne<50的相關數據 db.winner.find({winne:{$gte:40,$lt:45}}) { "_id" : ObjectId("592e7e14aaa464fa8a557ec4"), "winne" : 41 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec5"), "winne" : 42 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec6"), "winne" : 43 } { "_id" : ObjectId("592e7e14aaa464fa8a557ec7"), "winne" : 44 } { "_id" : ObjectId("592e7e17aaa464fa8a557f28"), "winne" : 41 } { "_id" : ObjectId("592e7e17aaa464fa8a557f29"), "winne" : 42 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2a"), "winne" : 43 } { "_id" : ObjectId("592e7e17aaa464fa8a557f2b"), "winne" : 44 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8c"), "winne" : 41 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8d"), "winne" : 42 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8e"), "winne" : 43 } { "_id" : ObjectId("592e7e18aaa464fa8a557f8f"), "winne" : 44 }
4、MongoDB AND 條件
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,語法格式如下:
>db.winner.find({key1:value1, key2:value2}).pretty()
#插入測試數據 for(i=0;i<20;i++)db.info2.insert({name:"linux", object:"SA", company:"docker", phone:i}) for(i=0;i<20;i++)db.info2.insert({name:"openstack", object:"DBA", company:"could", phone:i}) #檢查測試數據 > db.info2.find() db.info2.find() { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f838dd276944818f7edb9"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 5 } { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838dd276944818f7edbf"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 11 } { "_id" : ObjectId("592f838dd276944818f7edc0"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 12 } { "_id" : ObjectId("592f838dd276944818f7edc1"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 13 } { "_id" : ObjectId("592f838dd276944818f7edc2"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 14 } { "_id" : ObjectId("592f838dd276944818f7edc3"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 15 } { "_id" : ObjectId("592f838dd276944818f7edc4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 16 } { "_id" : ObjectId("592f838dd276944818f7edc5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 17 } { "_id" : ObjectId("592f838dd276944818f7edc6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 18 } { "_id" : ObjectId("592f838dd276944818f7edc7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 19 } >db.info2.find().count() #檢查數據的條數 40 #篩選name=linux object=SA phone<5 db.info2.find({name:"linux",object:"SA",phone:{$lt:5}}) 執行 db.info2.find({name:"linux",object:"SA",phone:{$lt:5}}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } 篩選name=linux object=SA 5<phone<=10 db.info2.find({name:"linux",object:"SA",phone:{"$gt":5,"$lte":10}}) db.info2.find({name:"linux",object:"SA",phone:{"$gt":5,"$lte":10}}) { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } 篩選 5<phone<=10 db.info2.find({phone:{"$gt":5,"$lte":10}}) { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838fd276944818f7edce"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 6 } { "_id" : ObjectId("592f838fd276944818f7edcf"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 7 } { "_id" : ObjectId("592f838fd276944818f7edd0"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 8 } { "_id" : ObjectId("592f838fd276944818f7edd1"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 9 } { "_id" : ObjectId("592f838fd276944818f7edd2"), "name" : "openstack", "object" : "DBA", "company" : "could", "phone" : 10 }
5 MongoDB OR 條件
MongoDB 除了有類似MySQL的AND條件語句外,還有OR 條件語句,OR 條件語句使用了關鍵字 $or,語法格式如下:
>db.collections.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
#查name=linux 或者object=redis db.info2.find( {$or:[{name:"linux"},{object:"redis"}] } ) db.info2.find(db.info2.find( ... {$or:[{name:"linux"},{object:"redis"}]{$or:[{name:"linux"},{object:"redis"}] ... ... }} ... )) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f838dd276944818f7edb9"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 5 } { "_id" : ObjectId("592f838dd276944818f7edba"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 6 } { "_id" : ObjectId("592f838dd276944818f7edbb"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 7 } { "_id" : ObjectId("592f838dd276944818f7edbc"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 8 } { "_id" : ObjectId("592f838dd276944818f7edbd"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 9 } { "_id" : ObjectId("592f838dd276944818f7edbe"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 10 } { "_id" : ObjectId("592f838dd276944818f7edbf"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 11 } { "_id" : ObjectId("592f838dd276944818f7edc0"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 12 } { "_id" : ObjectId("592f838dd276944818f7edc1"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 13 } { "_id" : ObjectId("592f838dd276944818f7edc2"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 14 } { "_id" : ObjectId("592f838dd276944818f7edc3"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 15 } { "_id" : ObjectId("592f838dd276944818f7edc4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 16 } { "_id" : ObjectId("592f838dd276944818f7edc5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 17 } { "_id" : ObjectId("592f838dd276944818f7edc6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 18 } { "_id" : ObjectId("592f838dd276944818f7edc7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 19 } Type "it" for more > itit { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede1"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 5 } { "_id" : ObjectId("592f8964d276944818f7ede2"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 6 } { "_id" : ObjectId("592f8964d276944818f7ede3"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 7 } { "_id" : ObjectId("592f8964d276944818f7ede4"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 8 } { "_id" : ObjectId("592f8964d276944818f7ede5"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 9 }
6、AND和OR綜合使用
查詢phone<5,name=MongoDB或者name=linux
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}) db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 }
7、查詢結果排序
在MySQL中是有order by條件,可以根據desc或者asc進行升序或者降序操作,而MongoDB中是可以利用sort()方法實現排序的,例如對6中的結果處理,根據phone排序。
基本語法
db.info2.find().sort({phone:1}) #這里phone表示根據該key排序,1表示升序,-1表示降序。
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 }
8、MongoDB Skip() 方法
在前面介紹了limit(),sort(),count()等方法,接下來要介紹一個比較有趣的skip()方法,在使用limit()的時候可以顯示你要求的幾條,而skip()方法是跳過幾條。
db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}) { "_id" : ObjectId("592f838dd276944818f7edb4"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 0 } { "_id" : ObjectId("592f8964d276944818f7eddc"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 0 } { "_id" : ObjectId("592f838dd276944818f7edb5"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 1 } { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } 使用skip()方法 db.info2.find({phone:{$lt:5},$or:[{name:"MongoDB"},{name:"linux"}]}).sort({phone:1}).skip(3) { "_id" : ObjectId("592f8964d276944818f7eddd"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 1 } { "_id" : ObjectId("592f838dd276944818f7edb6"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 2 } { "_id" : ObjectId("592f8964d276944818f7edde"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 2 } { "_id" : ObjectId("592f838dd276944818f7edb7"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 3 } { "_id" : ObjectId("592f8964d276944818f7eddf"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 3 } { "_id" : ObjectId("592f838dd276944818f7edb8"), "name" : "linux", "object" : "SA", "company" : "docker", "phone" : 4 } { "_id" : ObjectId("592f8964d276944818f7ede0"), "name" : "MongoDB", "object" : "redis", "company" : "winner", "phone" : 4 } skip 方法有點類似于MySQL里面的limit之間間隔情況。
這里介紹了有關查詢的問題,在數據庫中,查詢是非常重要的一部分,所以介紹的篇幅也是比較多的,后期遇到其他問題也會繼續總結輸出。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。