将IF语句应用于$ group

时间:2019-02-20 11:51:34

标签: mongodb

我想在此查询中放置一个IF语句条件,该条件基于顶部的变量。我想将 var line_item 设置为 true false 。根据line_item = false的条件,然后不要 $ unwind line_items ,否则不要$ unwind line_items。

我将如何为部分脚本编写不运行的IF语句?谢谢

var item_category = "sales-revenue";
var context = "item"; // item, customer, supplier, staff
var def_lang = "EN";
var metric_group = "month"; // year, month, date, hour, minute
const periods = {
  "year"  : 10000000000,
  "month" : 100000000,
  "date"  : 1000000,
  "hour"  : 10000,
  "minute": 100,

// if context = "" then the metric_group is by null

};
const collection = "9SP_Data";
const categories = {
  "sales-revenue": {
    "class": "revenue-transaction",
    "name" : "revenue",
    "category": "sales",
},
  "sales-expense": {
    "class": "expense-transaction",
    "name" : "expense",
    "category" : "sales",
},
  "payments": {
    "class" : "payment-transaction",
    "name": "payment",
    "category" : "payment",
}
};
const date_range = {
   start: 20160101000000,
   end: 20170101000000
};

const contexts = {
   "item"     : "$line_items.item_name",
   "customer" : "$party_identifier",
   "supplier" : "$party_identifier",
   "staff"    : "$staff_name",
};

// sums any line_item values of specific category by month, day, hour and even minute



db.getCollection(collection).aggregate([


{"$match" : {"_id.object_category" : categories[item_category].class,
"_id.transaction_date": {
    $gte: date_range.start,
    $lt: date_range.end
    },
    // also should include company
}},

// refine filter to line_item types and then apply calculations and lookups

{"$unwind" :  "$line_items"},
{"$match"  :  {"line_items.item_category": item_category}},
{"$group" : {
   "_id":
       {
        "company" : "$_id.connection",
        "sum_by_date":  {$trunc:{ $divide: ["$_id.transaction_date", periods[metric_group] ]}},
        "category" : "$line_items.item_category",
        "group_by" : contexts[context],
        "origin_category" : "$_id.object_origin_category",
        "object_origin_type" : "$_id.object_origin_type",
        "object_origin" : "$_id.object_origin"
           },
           "metric_value"  : { $sum: "$line_items.item_net_total_value" },
           "metric_volume"  : { $sum: "$line_items.item_quantity" }

// need to include a $count of documents within these groups NOT count of line_items as metric_volume

// saving space for a $lookup [name, category, def_lang and "definition"] from metrics to include global name and description in local language
}},
{"$project" : {
  "_id.company"               : "$_id.company",
  "_id.metric_name"           : {$literal : categories[item_category].name},
  "_id.metric_category"       : {$literal : categories[item_category].category},
  "_id.metric_type"           : {$literal : metric_group},
  "_id.metric_lookup"         : "$_id.sum_by_date",
  "_id.object_origin_category": "$_id.origin_category",
  "_id.object_origin_type"    : "$_id.object_origin_type",
  "_id.object_origin"         : "$_id.object_origin",
  "metric_group"              : "$_id.group_by",
  "metric_value"              : "$metric_value",
  "metric_currency"           : {$literal : "NZD"},  // currency not in current Vend extract I'm working with
  "metric_volume"             : "$metric_volume"     // only relevant why metric_group NOT null
   }}
])

0 个答案:

没有答案