Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表示mongoDB聚合中数组中某个位置的对象_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

表示mongoDB聚合中数组中某个位置的对象

表示mongoDB聚合中数组中某个位置的对象,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,在聚合中,我试图投影一个对象(它是一个数组),该对象位于数组中的位置7。例如,我有如下字段 { "$project": { "result": { "$cond": { "if": { "$eq": ["$coupon_type", 1] }, "then":

在聚合中,我试图投影一个
对象(它是一个数组)
,该对象位于数组中的
位置7。例如,我有如下字段

{
        "$project": {
            "result": {
                "$cond": {
                    "if": {
                        "$eq": ["$coupon_type", 1]
                    },
                    "then": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr"],
                    "else": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr", "$coupon_codes", {
                        indexes: conditions
                    }]
                }
            }
        }
    },
    {
        "$project": {
            obj: { $arrayElemAt: [ "$result", 7 ] } // here how to project indexes.

    }
所以现在对于
优惠券类型:0
我将把我的结果投影为
[“$id”、“$优惠券类型”、“$nonce”、“$total\u优惠券”、“$amount”、“$curr\u ctr”、“$优惠券代码”、{index:conditions}]

现在,我想在下一个管道中投影的字段是“索引”,所以我尝试编写
$arrayElemAt:[“$result”,7]
,这给了我第7个索引元素,这是正确的,但我应该如何表示第7个位置上的对象“索引”

简而言之,我想写这样的东西,
$arrayElemAt:[“$result”,7]。索引
,这不是指向对象的正确方式


任何人都可以告诉我如何做到这一点。

您可以通过添加第二个$project阶段来实现这一点:

    db.collection.aggregate([{
            "$project": {
                "result": {
                    "$cond": {
                        "if": {
                            "$eq": ["$coupon_type", 1]
                        },
                        "then": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr"],
                        "else": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr", "$coupon_codes", {
                            indexes: conditions
                        }]
                    }
                }
            }
        },
        {
            "$project": {
                obj: { $arrayElemAt: [ "$result", 7 ] } 

        }, 
        {"$project": {
                obj: "$obj.indexes" }
        } 
])

您可以组合项目阶段并使用
$let
来项目索引

$project: {
    indexes: {
        $let: {
            vars: {
                obj: {
                    $arrayElemAt: [{
                        "$cond": {
                            "if": {
                                "$eq": ["$coupon_type", 1]
                            },
                            "then": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr"],
                            "else": ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr", "$coupon_codes", {
                                indexes: conditions
                            }]
                        }
                    }, 7]
                }
            },
            in: "$$obj.indexes"
        }
    }
}

实际上很抱歉,我没有上传我的全部代码,我只想在优惠券类型等于0时投影obj,因为对于优惠券类型1,结果的第7个索引不存在。但是根据你的代码,即使是优惠券类型1,你也会尝试投影obj.index,所以你能在代码中更改它吗?我没有更改任何内容。甚至第二个项目中的代码也在做同样的事情。您可以将代码移动到第二个项目,并使用$let提取索引。这就是你想要的吗?你能告诉我一件事吗?假设我现在想在一个投影中执行三个操作,我如何使用$let来执行,那么我的问题将得到完美解决。我想用三倍的美元,但我不认为这会起作用。还非常感谢你帮我离开这里男:)$let用于存储中间结果,以便你可以应用到每个文档。看看这对你是否有意义。是的,在你的回答中我已经看到了,我真的很喜欢这个想法,我想问一下,假设我使用了<代码> var ava/c>,然后我想在< <代码>中添加代码2,然后我想在< <代码> >代码3中添加代码3。现在我可以在<代码> a < <代码>中使用这两个值改变操作,使用<代码> $/<代码>,因为我必须使用两次<代码> $ < <代码> >第一次将
a的值更改为2
,第二次将
的值更改为3
??我离解决方案还有最后一步,请帮助我,我知道这困扰着你。