在前面的文章里面主要介紹了MongoDB的文檔,集合,數(shù)據(jù)庫等操作和對文檔的增、刪、改相關(guān)知識,接下來會總結(jié)一點有關(guān)查詢的相關(guān)知識。
創(chuàng)新互聯(lián)專注于德惠網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供德惠營銷型網(wǎng)站建設(shè),德惠網(wǎng)站制作、德惠網(wǎng)頁設(shè)計、德惠網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造德惠網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供德惠網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。在MySQL中,我們知道數(shù)據(jù)查詢是優(yōu)化的主要內(nèi)容,讀寫分離等技術(shù)都是可以用來處理數(shù)據(jù)庫查詢優(yōu)化的,足以見數(shù)據(jù)庫查詢是每個系統(tǒng)中很重要的一部分,之前介紹了find的簡單使用,下面會介紹一些相對比較復(fù)雜一點的查詢。
一、數(shù)據(jù)查詢
MySQL數(shù)據(jù)庫中主要是用select 結(jié)合where子句實現(xiàn)數(shù)據(jù)的查詢,功能特別強大,例如多表聯(lián)合查詢、支持正則表達式等。不在這里做過多的相關(guān)介紹。這里主要介紹MongoDB的相關(guān)查詢,MongoDB中主要用find()實現(xiàn)數(shù)據(jù)的查詢,同時也可以使用一些條件限制。
1.1顯示單條數(shù)據(jù)
在上篇文章中提到了find()的使用,但是每次查詢數(shù)據(jù),都是查詢所有的,顯示其中的一部分,可以用it迭代。有時候我們想要查詢其中的一條數(shù)據(jù),具體操作要根據(jù)具體需求實現(xiàn)。
MongoDB 查詢數(shù)據(jù)的語法
db.collection.find(query, projection) query :可選,使用查詢操作符指定查詢條件
projection :可選,使用投影操作符指定返回的鍵。查詢時返回文檔中所有鍵值, 只需省略該參數(shù)即可(默認省略)??梢允褂?pretty() 方法以易讀的方式來讀取數(shù)據(jù),,語法格式如下
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、查詢某個集合中的所有數(shù)據(jù)
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條數(shù)據(jù),其他數(shù)據(jù)可以輸入it迭代。
2、顯示一條數(shù)據(jù)
find()是輸出所有結(jié)果,里面可能有些文檔內(nèi)容相同,但是“_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的一條數(shù)據(jù),而用find()查詢出所有的數(shù)據(jù),這時就可以使用findOne() db.winner.findOne({winne:1955}) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 或者可以使用 db.winner.find({winne:1955}).limit(1) { "_id" : ObjectId("592e7d1caaa464fa8a557e95"), "winne" : 1955 } 兩者的區(qū)別 findOne()有點類似MySQL里面的distinct,會返回查詢的第一條結(jié)果,如果搜索不到想要的數(shù)據(jù)就會 返回NULL, db.winner.findOne({winne:200888}) null db.winner.find({winne:1955}).limit(1)方法就和MySQL里面的limit是一樣的,主要是限制查詢結(jié)果的條數(shù)。3、查詢滿足一定條件的數(shù)據(jù)
在MySQL中查詢時,可以結(jié)合where以及字段等信息查詢數(shù)據(jù),而MongoDB中也是可以的,同樣可以支持一些條件判斷語句。
格式 | 范例 | RDBMS中的類似語句 | |
---|---|---|---|
等于 | { } | db.col.find({"winne":"1995"}).pretty() | where |
小于 | { | db.col.find({" | where |
小于或等于 | { | db.col.find({" | where |
大于 | { | db.col.find({" | where |
大于或等于 | { | db.col.find({" | where |
不等于 | { | db.col.find({" | where |
4、MongoDB AND 條件
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,語法格式如下:
>db.winner.find({key1:value1, key2:value2}).pretty()
5 MongoDB OR 條件
MongoDB 除了有類似MySQL的AND條件語句外,還有OR 條件語句,OR 條件語句使用了關(guān)鍵字 $or,語法格式如下:
>db.collections.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
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、查詢結(jié)果排序
在MySQL中是有order by條件,可以根據(jù)desc或者asc進行升序或者降序操作,而MongoDB中是可以利用sort()方法實現(xiàn)排序的,例如對6中的結(jié)果處理,根據(jù)phone排序。
基本語法
db.info2.find().sort({phone:1}) #這里phone表示根據(jù)該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之間間隔情況。這里介紹了有關(guān)查詢的問題,在數(shù)據(jù)庫中,查詢是非常重要的一部分,所以介紹的篇幅也是比較多的,后期遇到其他問題也會繼續(xù)總結(jié)輸出。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。