這篇文章給大家介紹MongoDB 中怎么計(jì)算子查詢,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
本溪ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
MongoDB在某些方面確實(shí)比關(guān)系型數(shù)據(jù)庫(kù)更強(qiáng) (比如對(duì)追加型日志數(shù)據(jù)的吞吐能力),但結(jié)構(gòu)化計(jì)算能力方面較弱。比如:MongoDB不支持子查詢,碰到這些復(fù)雜的運(yùn)算就只能先將數(shù)據(jù)讀出后再計(jì)算,而用Java等語(yǔ)言編寫(xiě)這類計(jì)算也不是很簡(jiǎn)單。
比如要處理這么個(gè)場(chǎng)景:查出訂單信息,要求訂單中的SELLERID必須是employee集合中STATE= California的員工id。如果寫(xiě)成sql就是:
Select * from orders where orders.sellerid in (select eid from employee where employee.state=’California’)。
orders的數(shù)據(jù)量較大無(wú)法一次取出,employee的數(shù)據(jù)量較小,最終結(jié)果的數(shù)據(jù)量也較小。部分?jǐn)?shù)據(jù)如下:
MongoDB Collection orders:
… { "_id" : ObjectId("5434f88dd00ab5276493e270"), "ORDERID" : 1, "CLIENT" : "UJRNP ","SELLERID" : 17, "AMOUNT" : 392, "ORDERDATE" : "2008/11/2 15:28" } { "_id" : ObjectId("5434f88dd00ab5276493e271"), "ORDERID" : 2, "CLIENT" : "SJCH" , "SELLERID" : 6, "AMOUNT" : 4802, "ORDERDATE" : "2008/11/9 15:28" } { "_id" : ObjectId("5434f88dd00ab5276493e272"), "ORDERID" : 3, "CLIENT" : "UJRNP ","SELLERID" : 16, "AMOUNT" : 13500, "ORDERDATE" : "2008/11/5 15:28" } { "_id" : ObjectId("5434f88dd00ab5276493e273"), "ORDERID" : 4, "CLIENT" : "PWQ", "SELLERID" : 9, "AMOUNT" : 26100, "ORDERDATE" : "2008/11/8 15:28" } … |
MongoDB Collection employee:
… { "_id" : ObjectId("5437413513bdf2a4048f3480"), "EID" : 1, "NAME" : "Rebecca", " SURNAME" : "Moore", "GENDER" : "F", "STATE" : "California", "BIRTHDAY" : "1974-1 1-20","HIREDATE" : "2005-03-11", "DEPT" : "R&D", "SALARY" : 7000 } { "_id" : ObjectId("5437413513bdf2a4048f3481"), "EID" : 2, "NAME" : "Ashley", "S URNAME" : "Wilson", "GENDER" : "F", "STATE" : "New York", "BIRTHDAY" : "1980-07- 19","HIREDATE" : "2008-03-16", "DEPT" : "Finance", "SALARY" : 11000 } { "_id" : ObjectId("5437413513bdf2a4048f3482"), "EID" : 3, "NAME" : "Rachel", "S URNAME" : "Johnson", "GENDER" : "F", "STATE" : "New Mexico", "BIRTHDAY" : "1970- 12-17","HIREDATE" : "2010-12-01", "DEPT" : "Sales", "SALARY" : 9000 } … |
計(jì)算結(jié)果:
如果有集算器的協(xié)助就簡(jiǎn)單多了,它是專門為結(jié)構(gòu)化、半結(jié)構(gòu)化數(shù)據(jù)計(jì)算設(shè)計(jì)的編程語(yǔ)言,查詢、子查詢、分組后過(guò)濾等等都有函數(shù)類庫(kù)可以用,能夠很好的彌補(bǔ)MongoDB在結(jié)構(gòu)化方面計(jì)算能力的不足。比如上面問(wèn)題,4行就搞定了:
A | |
1 | =mongo_open("mongodb://localhost:27017/test?user=test&password=test") |
2 | =mongo_shell(A1,"orders.find(,{_id:0})") |
3 | =mongo_shell@x(A1,"employee.find({STATE:'California'},{_id:0})").fetch() |
4 | =A2.select(A3.(EID).sort().pos@b(SELLERID)).fetch() |
其實(shí)還有很多情況用MongoDB處理結(jié)構(gòu)化計(jì)算不太方便,但有集算器SPL的輔助卻很簡(jiǎn)單,感興趣可以參考: 玩轉(zhuǎn)Mongo計(jì)算、 簡(jiǎn)化MongoDB關(guān)聯(lián)運(yùn)算、 輔助MongoDB計(jì)算
集算器還很容易嵌入到Java應(yīng)用程序中, Java如何調(diào)用SPL腳本有使用和獲得它的方法。
關(guān)于MongoDB 中怎么計(jì)算子查詢就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。