Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Represent object present at a position inside an array in mongoDB aggregation

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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" }
        } 
])
over 4 years ago · Santiago Trujillo Relatório

0

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"
        }
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda