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

188
Visualizações
Project different fields based on different condition

I am using aggregation in MongoDB and now I am facing a problem, I want to project my fields based on a condition matching or not.

For example I have a field coupon_type for which I will check if its value is equal to 1 then I will project fields ["curr_ctr", "total_coupons"] otherwise if its value is not equal to 1 then i will project fields ["curr_ctr", "total_coupons", "curr_ctr", "coupons"].

I can use two queries and run them in parallel but I am trying to achieve my result using one single query.

Can anyone please tell me how can I do this in one single query?

UPDATE

My documents are like below

[{ "_id" : ObjectId("5878e1edf1df5a2b69bcf60e"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1eff1df5a2b69bcf60f"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 0 } ,
{ "_id" : ObjectId("5878e1f1f1df5a2b69bcf610"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f3f1df5a2b69bcf611"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f5f1df5a2b69bcf612"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 11 } ,
{ "_id" : ObjectId("5878e1f7f1df5a2b69bcf613"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 110 },
{ "_id" : ObjectId("5878e1f9f1df5a2b69bcf614"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 0 } ]

Now for all coupon_type equal to 0 i want to project fields ["curr_ctr", "total_coupons", "curr_ctr", "coupons"] and for all coupon_type not equal to 0 i want to project fields ["curr_ctr", "total_coupons"]

UPDATE

Actually i want to project coupons array from index n to n+1, where n is the input by the user.

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

0

Let consider your collection contain following documents

[{ "_id" : ObjectId("5878e1edf1df5a2b69bcf60e"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1eff1df5a2b69bcf60f"), "coupon_type" : 0 } ,
{ "_id" : ObjectId("5878e1f1f1df5a2b69bcf610"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f3f1df5a2b69bcf611"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f5f1df5a2b69bcf612"), "coupon_type" : 11 } ,
{ "_id" : ObjectId("5878e1f7f1df5a2b69bcf613"), "coupon_type" : 110 },
{ "_id" : ObjectId("5878e1f9f1df5a2b69bcf614"), "coupon_type" : 0 } ]

Now you need to use $eq in project as :

db.collectoin.aggregate({
  "$project": {
    "result": {
      "$cond": {
    "if": {
      "$eq": ["$coupon_type", 1]
    },
    "then": ["curr_ctr", "total_coupons"],
    "else": ["curr_ctr", "total_coupons", "curr_ctr", "coupons"]
      }
    }
  }
})

As per new Update you should modify query as like this :

db.collection.aggregate({
  "$project": {
    "result": {
      "$cond": {
    "if": {
      "$eq": ["$coupon_type", 1]
    },
    "then": ["$curr_ctr", "$total_coupons", "$coupons"],
    "else": ["$curr_ctr", "$total_coupons"]
      }
    }
  }
})

UPDATE

As per new update let consider your n given by user for ex. : var n = 1;

now query will be as below :

db.coupon.aggregate({
  "$project": {
    "result": {
      "$cond": {
    "if": {
      "$eq": ["$coupon_type", 1]
    },
    "then": ["$curr_ctr", "$total_coupons", {
      "coupons": {
        "$slice": ["$coupons", n, n + 1]
      }
    }],
    "else": ["$curr_ctr", "$total_coupons"]
      }
    }
  }
})
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