Represent object present at a position inside an array in mongoDB aggregation

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

In aggregation i am trying to project an object(which is an array) which is present at position 7 inside an array. For example i have fields as below

{         "$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.      }

So now for coupon_type: 0 i will project my result as ["$_id", "$coupon_type", "$nonce", "$total_coupons", "$amount", "$curr_ctr", "$coupon_codes", { indexes: conditions}].

Now the field i want to project in my next pipeline is 'indexes' so i tried to write $arrayElemAt: [ "$result", 7 ] which is giving me 7th index element which is right but how should i represent object 'indexes' present at this 7th position.

In short i want to write something like this $arrayElemAt: [ "$result", 7 ].indexes which is not the right way to point to an abject.

Can anyone please tell me how can i do this.

回答1:

You can combine the project stages and use $let to project the index.

$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"         }     } }


回答2:

you can achieve this by adding a second $project stage:

    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" }         }  ])


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!