乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      實(shí)現(xiàn) MongoDB 外鍵關(guān)聯(lián)

       raqsoft 2019-10-21

      來源:http:///questions/29392169/populating-field-values-for-referred-documents-in-aggregate-call-in-mongoose

      MongoDB 是一個(gè)分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),也是 nosql 中最像關(guān)系型數(shù)據(jù)庫(kù)的一種。但是 mongodb 采用文檔模式設(shè)計(jì)的,這意味著集合中的文檔可以有相同或不同的字段,因此在關(guān)系型數(shù)據(jù)庫(kù)非常擅長(zhǎng)的多表關(guān)聯(lián)方面就顯得差強(qiáng)人意。如果采用 Mongodb 本身的 API 則需要硬編碼才能實(shí)現(xiàn)外鍵關(guān)聯(lián),不夠直觀且難度較大,這種情況下可以用集算器 SPL 語(yǔ)言來實(shí)現(xiàn),下面用例子說明。

              Collection UserCourseProgress 記錄著用戶和課程的關(guān)系,其courseid字段是外鍵,指向Collection Course的_id字段。需要統(tǒng)計(jì)出每門課的人數(shù),其中課程名稱需要使用Course的title字段進(jìn)行顯示。

      UserCourseProgressCourse
      {"userId":"u01",
      "courseid":"c01",
      "timespent":6000,
      score:99}
      {"userId":"u02",
      "courseid":"c01",
      "timespent":6000,
      score:99}
      {"userId":"u03",
      "courseid":"c01",
      "timespent":6000,
      score:99}
      {"userId":"u04",
      "courseid":"c01",
      "timespent":6000,
      score:99}
      {"userId":"u05",
      "courseid":"c01",
      "timespent":6000,
      score:99}
      {"userId":"u01",
      "courseid":"c02",
      "timespent":6000,
      score:99}
      {"userId":"u02",
      "courseid":"c02",
      "timespent":6000,
      score:99}
      {"userId":"u03",
      "courseid":"c03",
      "timespent":6000,
      score:99}
      {"_id":"c01"
      "title":"Japanese159",
      "description":"Japanese   base",
      "category":"language"}
      {"_id":"c02"
      "title":"Chinese200",
      "description":"Chinese   middle",
      "category":"language"}
      {"_id":"c03"
      "title":"Political   science 280",
      "description":"Political   middle",
      "category":"politics"}
      {"_id":"c04"
      "title":"EE490",
      "description":"electronic   engineering hign",
      "category":"Electronic"}

              使用集算器SPL的代碼如下:


      A
      1=mongo_open("mongodb://localhost:27017/local?user=test&password=test")
      2=mongo_shell(A1,"UserCourseProgress.aggregate([{$group:   {_id: {'primary':'$courseid'},'popularityCount': {$sum: 1}}}, {$sort:{'popularityCount':-1}},{$project:{_id:0,'courseid':'$_id.primary','popularityCount':1}}])")
      3=mongo_shell(A1,"Course.find(,{title:1})").fetch()
      4=A2.switch(courseid,A3:_id)
      5=A4.new(popularityCount,courseid.title)
      6=mongo_close(A1)

              A1: 連接MongoDB,連接字格式為mongo://ip:port/db?arg=value&…。

              A2: 統(tǒng)計(jì)出每門課的人數(shù)。這里使用MongoDB聚合函數(shù)aggregate在UserCourseProgress中取數(shù),參數(shù)是遵循mongodb規(guī)范的匯總表達(dá)式。計(jì)算結(jié)果是內(nèi)存數(shù)據(jù),如下:

              Picture 1

              A3: 用find函數(shù)從Course中取數(shù),過濾條件為空。Find的結(jié)果是游標(biāo),由于課程數(shù)量較少,因此用可以fetch函數(shù)將游標(biāo)讀入內(nèi)存,結(jié)果如下:

               Picture 4

               A4: 使用switch函數(shù)將A3中的外鍵切換為A2中的記錄,結(jié)果如下:

               Picture 7

              A5:按對(duì)象方式訪問內(nèi)存,形成新的二維表,結(jié)果如下:

              Picture 10

              A6:關(guān)閉mongodb連接。

              通過上面的例子可以看到,借助集算器SPL語(yǔ)言,在Mongodb中多個(gè)表的外鍵關(guān)聯(lián)查詢,完全可以做到像關(guān)系數(shù)據(jù)庫(kù)那樣方便、容易、直觀,不必?fù)?dān)心因?yàn)镸ongodb是非關(guān)系型數(shù)據(jù)庫(kù)而帶來的表間弱關(guān)聯(lián)的影響。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多