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

194
Visualizações
Find mongodb docs based on array of object contains two fields

Example: Mongo db collection:

[
  {
    "user_id": 1,
    "lead_id": 901,
     ...
     ...
  },
  {
    "user_id": 2,
    "lead_id": 902,
     ...
     ...
  },
   {
    "user_id": 2,
    "lead_id": 903,
    ...
    ...
  },
 {
    "user_id": 3,
    "lead_id": 999,
    ...
    ...
  },
]

I have an array of objects:

const reqArr = [
  {
    "user_id": 1,
    "lead_id": 901,
  }, 
   {
    "user_id": 2,
    "lead_id": 999,
   }
]

I want to query the DB based on both user_id and lead_id present in each object in the array reqArr. something like this or with aggregate pipeline

Model.find(reqArr)

My desired output would be something like this:

[{
 "user_id": 1,
 "lead_id": 901,
  ...
  ...
}]

I only know how to query based on the array of one field like Model.find({_id: {"$in": [array of ids ]}}) but not with more than one field in an array of object. Any idea or workaround to achieve something like the above output or negation of the above output? I only need which objects in reqArr are present in db collection or which are not. Thank you.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The simplest way to do the above would be this:

const results = await Promise.all(reqArr.map(req => {
    const { user_id, lead_id } = req;
    return Model.findOne({ user_id, lead_id });
}));

We map over each item in reqArr. For each item (req), we destructure into two variables user_id and lead_id.

We then query for a single document (I assume there is only one document for each user_id and lead_id pair, otherwise this will need to be slightly adjusted.

We then return the findOne promise, resulting in an array of promises which we pass into Promise.all to await them all asynchronously.

This should result flat array of results that match the lead_iq/user_id pairs in reqArr.

If some of these combos aren't present in the database, those elements of the array will be undefined and you can go on to handle those however you need to.

about 4 years ago · Juan Pablo Isaza Relatório

0

simple $or

db.collection.aggregate([
  {
    "$match": {
      "$or": [
        {
          "user_id": 1,
          "lead_id": 901
        },
        {
          "user_id": 2,
          "lead_id": 902
        }
      ]
    }
  }
])

mongoplayground


node

const reqArr = [
  {
    "user_id": 1,
    "lead_id": 901,
  }, 
   {
    "user_id": 2,
    "lead_id": 999,
   }
]
Model.find({ "$or": reqArr})
about 4 years ago · Juan Pablo Isaza 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