2017-07-11 6 views
0
{ 
    "_id" : ObjectId("590b12b6330e1567acd29e69"), 
    "name": "Foo", 
    "sales_history" : [ 
     { 
      "_id" : ObjectId("593ce8e4cfaa652df543d9e3"), 
      "sold_at" : ISODate("2017-06-11T06:53:24.881Z"), 
      "sold_to" : ObjectId("593509e938792e046ba14a02"), 
      "sold_products" : [ 
       { 
        "product_dp" : 100, 
        "quantity" : 1, 
        "product_id" : ObjectId("591068be1f4c6c79a442a788"), 
        "_id" : ObjectId("593ce8e4cfaa652df543d9e5") 
       }, 
       { 
        "product_dp" : 100, 
        "quantity" : 1, 
        "product_id" : ObjectId("593a33dccfaa652df543d924"), 
        "_id" : ObjectId("593ce8e4cfaa652df543d9e4") 
       } 
      ] 
     }, 
     { 
      "_id" : ObjectId("5944cb7142a04740357020b9"), 
      "sold_at" : ISODate("2017-06-17T06:25:53.332Z"), 
      "sold_to" : ObjectId("5927d4a59e58ba0c61066f3b"), 
      "sold_products" : [ 
       { 
        "product_dp" : 500, 
        "quantity" : 1, 
        "price" : 5650, 
        "product_id" : ObjectId("593191ed53a2741dd9bffeb5"), 
        "_id" : ObjectId("5944cb7142a04740357020ba") 
       } 
      ] 
     } 
    ] 
} 

私はこのようなユーザースキーマを持っています。私は、product_id参照の詳細と、日付範囲の検索条件をsold_at日付フィールドに指定します。

私がsold_atで検索する場合は、次のように私の予想データ:2017年6月11日

{ 
    "_id" : ObjectId("590b12b6330e1567acd29e69"), 
    "name": "Foo", 
    "sales_history" : [ 
     { 
      "_id" : ObjectId("593ce8e4cfaa652df543d9e3"), 
      "sold_at" : ISODate("2017-06-11T06:53:24.881Z"), 
      "sold_to" : ObjectId("593509e938792e046ba14a02"), 
      "sold_products" : [ 
       { 
        "product_dp" : 100, 
        "quantity" : 1, 
        "product_id": { 
         _id:ObjectId("hsfgg123412yh3gy1u2g3"), 
         name: "Product1", 
         code: "FG0154" 
         }, 
       } 
      ] 
      } 
     ] 
     } 

製品の詳細は、PRODUCT_IDに移入する必要があり、SALES_HISTORY配列は、日付範囲でフィルタする必要があります。

答えて

2

以下の集計クエリを試すことができます。 sales history & sold_productsをINGの$unwind続い日付範囲に

$filter

sales history

$lookupsold_products

$groupバックsold_products & sales history

db.collection.aggregate([ 
    { 
    "$project": { 
     "name": 1, 
     "sales_history": { 
     "$filter": { 
      "input": "$sales_history", 
      "as": "history", 
      "cond": { 
      "$and": [ 
       { 
       "$gte": [ 
        "$$history.sold_at", 
        ISODate("2017-06-11T00:00:00.000Z") 
       ] 
       }, 
       { 
       "$lt": [ 
        "$$history.sold_at", 
        ISODate("2017-06-12T00:00:00.000Z") 
       ] 
       } 
      ] 
      } 
     } 
     } 
    } 
    }, 
    { 
    "$unwind": "$sales_history" 
    }, 
    { 
    "$unwind": "$sales_history.sold_products" 
    }, 
    { 
    "$lookup": { 
     "from": lookupcollection, 
     "localField": "sales_history.sold_products.product_id", 
     "foreignField": "_id", 
     "as": "sales_history.sold_products.product_id" 
    } 
    }, 
    { 
    "$group": { 
     "_id": { 
     "_id": "$_id", 
     "sales_history_id": "$sales_history._id" 
     }, 
     "name": { 
     "$first": "$name" 
     }, 
     "sold_at": { 
     "$first": "$sales_history.sold_at" 
     }, 
     "sold_to": { 
     "$first": "$sales_history.sold_to" 
     }, 
     "sold_products": { 
     "$push": "$sales_history.sold_products" 
     } 
    } 
    }, 
    { 
    "$group": { 
     "_id": "$_id._id", 
     "name": { 
     "$first": "$name" 
     }, 
     "sales_history": { 
     "$push": { 
      "_id": "$_id.sales_history_id", 
      "sold_at": "$sold_at", 
      "sold_to": "$sold_to", 
      "sold_products": "$sold_products" 
     } 
     } 
    } 
    } 
]);