使用聚合框架在mongodb上查询的更快捷方式

时间:2016-03-08 16:50:53

标签: mongodb mongodb-query aggregation-framework

有没有更快的方法来查询MongoDB? 我的数据是这样的:

[{
        "c_custkey": 1,
        "c_name": "Customer#000000001",
        "c_address": "IVhzIApeRb",
        "c_city": "MOROCCO  0",
        "c_nation": "MOROCCO",
        "c_region": "AFRICA",
        "lineorders": [{
                    "lo_orderkey": 164711,
                    "lo_linenumber": 1,
                    "lo_custkey": 1,
                    "lo_partkey": 82527,
                    "lo_suppkey": 1848,
                    "lo_quantity": 34,
                    "lo_extendedprice": 5132368,
                    "orderdate": [{
                        "d_datekey": 19920426,
                        "d_date": "April 26, 1992",
                        "d_dayofweek": "Monday",
                        "d_month": "April",
                        "d_year": 1992,
                        "d_yearmonthnum": 199204,
                    }],
                    "part": [{
                        "p_partkey": 82527,
                        "p_name": "steel tomato",
                        "p_mfgr": "MFGR#4",
                        "p_category": "MFGR#45",
                        "p_brand1": "MFGR#452",
                    }],
                    "supplier": [{
                        "s_city": "MOZAMBIQU8",
                        "s_nation": "MOZAMBIQUE",
                        "s_region": "AFRICA",
                    }]
                }, {
                    "lo_orderkey": 164711,
                    "lo_linenumber": 2,
                    "lo_custkey": 1,
                    "lo_partkey": 26184,
                    "lo_suppkey": 1046,
                    "lo_orderdate": 19920426,
                    "lo_quantity": 15,
                    "lo_extendedprice": 1665270,
                    "orderdate": [{
                        "d_datekey": 19920426,
                        "d_date": "April 26, 1992",
                        "d_dayofweek": "Monday",
                        "d_month": "April",
                        "d_year": 1992,
                        "d_yearmonthnum": 199204,
                    }],
                    "part": [{
                        "p_partkey": 26184,
                        "p_name": "chartreuse green",
                        "p_mfgr": "MFGR#2",
                        "p_category": "MFGR#23",
                        "p_brand1": "MFGR#2329",
                    }],
                    "supplier": [{
                        "s_suppkey": 1046,
                        "s_city": "SAUDI ARA2",
                        "s_nation": "SAUDI ARABIA",
                        "s_region": "MIDDLE EAST",
                    }]
                },

我正在这样查询:

db.customer.aggregate([
   { "$match":{
       "$and":[
          {"lineorder.orderdate.d_year":{"$gte":1992}},
          {"lineorder.orderdate.d_year":{"$lte":1997}},
          {"lineorder.supplier.s_region":"ASIA"},
          {"c_region":"ASIA"}
       ]
   }},
   { "$unwind":"$lineorder" },
   { "$match":{
       "$and":[
           {"lineorder.orderdate.d_year":{"$gte":1992}},
           {"lineorder.orderdate.d_year":{"$lte":1997}},
           {"lineorder.supplier.s_region":"ASIA"},
           {"c_region":"ASIA"}
       ]
    }},
   { "$project": {
       "lineorder.supplier.s_nation":1,
       "c_nation":1,"
       "lineorder.orderdate.d_year":1,
       "lineorder.lo_revenue":1
   }},
   { "$group":{
      "_id":{
          "s_nation":"$lineorder.supplier.s_nation",
          "c_nation":"$c_nation",
          "d_year": "$lineorder.orderdate.d_year"
      }, 
      "revenue": {"$sum": "$lineorder.lo_revenue"}
   }},
   { "$sort":{"_id.d_year":1, revenue:-1}}
]).toArray()

我必须“匹配”两次的原因是因为当我这样做时它会返回符合我条件的所有文档,而不是所有符合条件的文档中的所有“lineorders”都满足它,如果我这样做了在需要花费大量时间之前“放松”。我的数据库有2 GB这种数据。

0 个答案:

没有答案