2015-04-17 113 views
1

seasons.jsonMongoDB中找到查詢

{ 
    "_id" : "unique_1", 
    "spring" : [{ 
     "fruit" : "mango", 
     "person_id" : [101.0, 102.0, 103.0, 104.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [151.0, 152.0, 153.0, 154.0] 
    }], 
    "summer" : [{ 
     "fruit" : "mango", 
     "person_id" : [201.0, 202.0, 203.0, 204.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [251.0, 252.0, 253.0, 254.0] 
    }], 
    "fall" : [{ 
     "fruit" : "mango", 
     "person_id" : [301.0, 302.0, 303.0, 304.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [351.0, 352.0, 353.0, 354.0] 
    }], 
    "winter" : [{ 
     "fruit" : "mango", 
     "person_id" : [401.0, 402.0, 403.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [451.0, 452.0, 453.0] 
    }] 
} 

/* 2 */ 
{ 
    "_id" : "unique_2", 
    "spring" : [{ 
     "fruit" : "banana", 
     "person_id" : [151.0, 152.0, 153.0, 154.0] 
    }], 
    "summer" : [{ 
     "fruit" : "mango", 
     "person_id" : [201.0, 202.0, 203.0, 204.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [251.0, 252.0, 253.0, 254.0] 
    }], 
    "fall" : [{ 
     "fruit" : "banana", 
     "person_id" : [351.0, 352.0, 353.0, 354.0] 
    }], 
    "winter" : [{ 
     "fruit" : "mango", 
     "person_id" : [401.0, 402.0, 403.0] 
    }, { 
     "fruit" : "banana", 
     "person_id" : [451.0, 452.0, 453.0] 
    }] 
} 

以上JSON記錄顯示哪個季節哪個人吃了芒果和具有食用香蕉。

這是我想要找什麼: 當我知道_id預先記錄的(主鍵)或記錄發現之前 -

1)全部爲person_id從101 - 350中爲person_id是獨特 2)person_id只吃芒果 3)吃水果芒果或香蕉記錄的人總數。

+0

請出示你的努力(即代碼)第一。 –

+0

我在特定的fruit-db.season.find({「spring.person_id」:{「$ elemMatch」:{「$ gt」:101,「$ lt」:104}}}中找出了person_id的範圍。 ); – joshi

回答

3

對於這樣的模式,運行像您所需要的那樣的性質的查詢將會非常困難。考慮改變模式,使得每個子文檔都有一個主鍵,例如seasons,其可以具有四個不同的陣列元素,即春季,夏季,冬季和秋季。架構改成這樣:

/* 1 */ 
{ 
    "_id" : "unique_1", 
    "seasons" : [ 
     { 
      "name" : "spring", 
      "fruits" : [ 
       { 
        "name" : "mango", 
        "person_id" : [ 
         101, 
         102, 
         103, 
         104 
        ] 
       }, 
       { 
        "name" : "banana", 
        "person_id" : [ 
         151, 
         152, 
         153, 
         154 
        ] 
       } 
      ] 
     }, 
     { 
      "name" : "summer", 
      "fruits" : [ 
       { 
        "name" : "mango", 
        "person_id" : [ 
         201, 
         202, 
         203, 
         204 
        ] 
       }, 
       { 
        "name" : "banana", 
        "person_id" : [ 
         251, 
         252, 
         253, 
         254 
        ] 
       } 
      ] 
     }, 
     { 
      "name" : "fall", 
      "fruits" : [ 
       { 
        "name" : "mango", 
        "person_id" : [ 
         301, 
         302, 
         303, 
         304 
        ] 
       }, 
       { 
        "name" : "banana", 
        "person_id" : [ 
         351, 
         352, 
         353, 
         354 
        ] 
       } 
      ] 
     }, 
     { 
      "name" : "winter", 
      "fruits" : [ 
       { 
        "name" : "mango", 
        "person_id" : [ 
         401, 
         402, 
         403 
        ] 
       }, 
       { 
        "name" : "banana", 
        "person_id" : [ 
         451, 
         452, 
         453 
        ] 
       } 
      ] 
     } 
    ] 
} 

有了這個模式就變得更容易運行下面的聚集查詢:

1)全部爲person_id從101 - 350,其中PERSON_ID是唯一

var pipeline1 = [ 
    { "$match": { "_id": "unique_1" }, 
    { "$unwind": "$seasons" }, 
    { "$unwind": "$seasons.fruits" }, 
    { "$unwind": "$seasons.fruits.person_id" }, 
    { 
     "$match": { 
      "seasons.fruits.person_id": { 
       "$gte": 101, 
       "$lte": 350 
      } 
     } 
    },  
    { 
     "$group": { 
      "_id": 0, 
      "person_ids": { 
       "$addToSet": "$seasons.fruits.person_id" 
      } 
     } 
    }, 
    { 
     "$project": { 
      "_id": 0, 
      "person_ids": 1 
     } 
    } 
]; 

db.season.aggregate(pipeline1); 

輸出

/* 1 */ 
{ 
    "result" : [ 
     { 
      "person_ids" : [ 
       304, 
       253, 
       201, 
       251, 
       301, 
       203, 
       252, 
       204, 
       152, 
       102, 
       202, 
       154, 
       254, 
       101, 
       302, 
       153, 
       104, 
       103, 
       303, 
       151 
      ] 
     } 
    ], 
    "ok" : 1 
} 

2)PERSON_ID只吃芒果

var pipeline2 = [ 
    { "$match": { "_id": "unique_1" }, 
    { "$unwind": "$seasons" }, 
    { "$unwind": "$seasons.fruits" }, 
    { "$unwind": "$seasons.fruits.person_id" }, 
    { 
     "$match": { 
      "seasons.fruits.name": "mango" 
     } 
    },  
    { 
     "$group": { 
      "_id": 0, 
      "person_ids": { 
       "$addToSet": "$seasons.fruits.person_id" 
      } 
     } 
    }, 
    { 
     "$project": { 
      "_id": 0, 
      "person_ids": 1 
     } 
    } 
]; 

db.season.aggregate(pipeline2); 

輸出

/* 1 */ 
{ 
    "result" : [ 
     { 
      "person_ids" : [ 
       402.0000000000000000, 
       304.0000000000000000, 
       303.0000000000000000, 
       302.0000000000000000, 
       301.0000000000000000, 
       204.0000000000000000, 
       202.0000000000000000, 
       201.0000000000000000, 
       203.0000000000000000, 
       104.0000000000000000, 
       102.0000000000000000, 
       103.0000000000000000, 
       403.0000000000000000, 
       401.0000000000000000, 
       101.0000000000000000 
      ] 
     } 
    ], 
    "ok" : 1 
} 

3)的人總數在記錄吃水果或者芒果或香蕉。

var pipeline3 = [ 
    { "$match": { "_id": "unique_1" }, 
    { "$unwind": "$seasons" }, 
    { "$unwind": "$seasons.fruits" }, 
    { "$unwind": "$seasons.fruits.person_id" }, 
    { 
     "$match": { 
      "seasons.fruits.name": { 
       "$in": ["mango", "banana"] 
      } 
     } 
    },  
    { 
     "$group": { 
      "_id": "$_id", 
      "count": { 
       "$sum": 1 
      } 
     } 
    }, 
    { 
     "$project": { 
      "_id": 0, 
      "count": 1 
     } 
    } 
]; 

db.season.aggregate(pipeline3); 

輸出

/* 1 */ 
{ 
    "result" : [ 
     { 
      "count" : 30 
     } 
    ], 
    "ok" : 1 
} 
+1

chrisdam - 感謝您的詳細解答。但是,在您的帖子中粘貼聚合查詢方法時會發生某些錯誤。它沒有正常運行。如果您通過詳細的說明解釋上述任何一項查詢,我將非常感謝您。 – joshi

+0

@joshi不用擔心伴侶。在運行聚合查詢之前,您是否設法更改架構?抱歉,我不能很好地解釋每條聚合流水線的含義,因爲您需要的查詢數量非常少,我認爲您知道聚合框架概念,因此我深入瞭解了實現的原因。 – chridam

+1

@chrisdam - 是的,我在db模式中做了這種改變。但無法運行聚合查詢。一些括號從我認爲是的位置錯位。因爲我對這個商店感到陌生並試圖採用自己的MongoDB框架。因此,對我來說,在彙總查詢中特別容易地查看代碼,但我非常感謝您的快速回復。 – joshi