在mongodb的聚合内调用函数? [英] Call function inside mongodb's aggregate?

查看:207
本文介绍了在mongodb的聚合内调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收藏:

[
    { _id: "Foo", flag1: false, flag2: true, flag3: false },
    { _id: "Bar", flag1: true, flag2: false, flag3: true }
]

我的问题是,是否可以在聚合查询中调用方法?

My question is, is it possible to call a method inside aggregate query?

aggregate({
    $project: {
        '_id': 1,
        'status' MyService.getStatus($flag1, $flag2, $flag3)
    }
});

如果可能的话,它的语法是什么?结果:

If it is possible, what is the syntax of it? Result:

[
    { _id: "Foo", status: 'ok' },
    { _id: "Bar", status: 'broken' }
]


在我的实际应用程序中,每个文档有10个布尔标志.如果用户获得了此文档,我想转换这些标志并给它们一个含义(对于用户).例如.考虑一个文件代表一个轮胎.


In my real world application I have 10 boolean flags per document. If the user gets this documents I would like to convert the flags and give them a meaning (for the user). E.g. consider a document represents a tire.

flag1 = true means tire have good pressure, false means low pressure
flag2 = true means depth of tire profile is good, false means little profile
and so on

因此,总而言之,我想说

So in summary I would like to say a tire is OK if

 flag1, flag2 are true and flag3 is false

在以下情况下需要更换轮胎(破损或更换)

and a tire needs to be replaced (BROKEN or REPLACE) when

flag1, flag2 are false and flag3 is true

将文档退还给用户时,应删除标志.取而代之的是,我们在状态字段中显示轮胎是正常"或破损".

When a document is returned to the user the flags should be removed. Instead we have the status field that says the tire is either OK or BROKEN.

推荐答案

外部功能不适用于聚合框架.一切都会在输入时解析为BSON,因此不允许使用JavaScript或其他任何东西.从BSON运算符"定义到本机C ++代码实现,所有这些基本上都得到了处理,因此它确实非常快.

External functions don't work with the aggregation framework. Everything is parsed to BSON on input, so no JavaScript or anything else is allowed. This is all basically processed from BSON "operator" definition to native C++ code implementation so it is really fast.

这归结为将您期望的逻辑转换"为聚合框架可以处理的逻辑.实际上存在逻辑"运算符,例如 $or $and 在这种情况下有效:

What this comes down to is "converting" your expected logic to what the aggregation framework can process. There are in fact "logical" operators such as $or and $and that work in this context:

db.collection.aggregate([
    { "$project": {
       "_id": 1,
       "status": {
           "$cond": [
               { "$or": [
                   // Your first set of rules requires "false" for "flag1" or 
                   // "flag2" and "true" for "flag3"
                   { "$and": [
                       { "$not": [
                           { "$or": [ "$flag1", "$flag2" ] },
                       ]},
                       "$flag3"
                   ]},
                   // Your second set of rules requires "true" for "flag1" or 
                   // "flag2" and "false" for "flag3"
                   { "$and": [
                       { "$or": [ "$flag1", "$flag2" ] },
                       { "$not": [ "$flag3" ] }
                   ]},
               ]},
               "ok",
               "broken"
           ]
       }
    }}
])

因此,没有外部功能,只需使用聚合框架提供的运算符来实现逻辑.除了基本的逻辑实现之外,还有 $not > 来反转"斜体和 $cond 用作三元" ,以提供不同的true/false评估的结果.

So no external functions, just implement the logic with the operators that the aggregation framework supplies. In addition to the basic logical implementations there is $not to "reverse" the ligic and $cond which acts as a "ternary" in order to provide a different result from true/false evaluation.

这篇关于在mongodb的聚合内调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆