2017-07-11 44 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

您可以嘗試下面的聚合查詢。

$filtersales history日期範圍,隨後$unwind荷蘭國際集團sales history & sold_products

$lookupsold_products獲取產品詳細信息。

$groupsold_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" 
     } 
     } 
    } 
    } 
]); 
相關問題