MongoDB条件聚合

时间:2015-02-26 10:57:59

标签: mongodb aggregation-framework

我收藏了,在里面我必须根据条件计算一个价格,如果选定的日期(即从2015/01/20到2015/01/22,3人)有不同的价格,如果没有,那么添加一个pricing.basic值加上如果有更多的人在人群中包含我需要乘以。

样品采集:

{
    "_id" : ObjectId("5473572cf1a76349020041c6"),
    "calendar" : [ 
        {
            "date" : ISODate("2015-01-20T12:41:20.000Z"),
            "status" : true
        }, 
        {
            "date" : ISODate("2015-01-21T12:41:20.000Z"),
            "status" : true,
            "pricing": {
                "basic": 9900,
                "perPerson": 1100
            }
        }, 
        {
            "date" : ISODate("2015-01-22T12:41:20.000Z"),
            "status" : true
        }, 
        {
            "date" : ISODate("2015-01-23T12:41:20.000Z"),
            "status" : false
        }, 
        {
            "date" : ISODate("2015-01-24T12:41:20.000Z"),
            "status" : false
        }, 
        {
            "date" : ISODate("2015-01-25T12:41:20.000Z"),
            "status" : false
        }
    ],
    "pricing" : {
        "basic" : 9470,
        "currency" : "USD",
        "personsIncluded" : 2,
        "perPerson" : 1000
    }
}

我在寻找:

(
    if $calendar.date = currentSearchDate && $calendar.pricing
        $calendar.pricing + (($pricing.personsIncluded-personsInputValue) * ($calendar.perPerson ?: $pricing.perPerson))
    else
        ($pricing.basic + (($pricing.personsIncluded-personsInputValue) * $pricing.perPerson ))
) + (
    if $calendar.date = currentSearchDate2 && $calendar.pricing
        $calendar.pricing + (($pricing.personsIncluded-personsInputValue) * ($calendar.perPerson ?: $pricing.perPerson))
    else
        ($pricing.basic + (($pricing.personsIncluded-personsInputValue) * $pricing.perPerson ))
) + (
    if $calendar.date = currentSearchDate3 && $calendar.pricing
        $calendar.pricing + (($pricing.personsIncluded-personsInputValue) * ($calendar.perPerson ?: $pricing.perPerson))
    else
        ($pricing.basic + (($pricing.personsIncluded-personsInputValue) * $pricing.perPerson ))
) + ...

当然只有选项是聚合框架,但它可以做这样的事情,或者它可能会更好地获得所有结果(当然过滤),将它们推入临时收集然后排序&过滤

1 个答案:

答案 0 :(得分:0)

查看该帖子:

MongoDB conditional aggregation

我已发布条件聚合,条件过滤器仍然缺失。

这可能会对你有所帮助。 如果你仍然需要帮助来建立你的聚合让我知道,我会尽我所能来帮助你。