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

查看:25
本文介绍了在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

所以总而言之,如果

 flag1, flag2 are true and flag3 is false

并且需要更换轮胎(BROKEN 或 REPLACE)

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

flag1, flag2 are false and flag3 is true

当文档返回给用户时,标志应该被移除.相反,我们有一个状态字段,表示轮胎是 OK 或 BROKEN.

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 充当 "ternary" 以提供与 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天全站免登陆