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

147
Visualizações
How do I map array of objects to an existing array on MongoDB

Please how can I make this work on mongoDB. For each item in an array, fetch data which contains the item from another collection.

For example:

"users" : [{ "name": "John doe", "age": 51 }, { "name": "Jake kim", "age": 50 }, { "name": "Jim son", "age": 51 }]

On another collection I have

"age": [50,51,52,53,54]

Now my desire result is

"50" : [{ "name": "Jake kim", "age": 50 }], "51" : [{ "name": "John doe", "age": 51 }, { "name": "Jim son", "age": 51 }]

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

0

You can do this. Get the array of users.

const users = [{ "name": "John doe", "age": 51 }, { "name": "Jake kim", "age": 50 }, { "name": "Jim son", "age": 51 }]

Get the array of ages

const age = [50,51,52,53,54]

Then you can map through the array of ages, returning an object that has the user that has the same age, here is the algorithm.

const mapped = age.map(age => {
  const user = {}
  user[age] = users.find(ob => ob.age === age);
  return user;
})

If you print out the mapped result. This is what it will look like, For the ages that we couldn't find their users, they just have an undefined value.

    console.log(mapped)
    [
      { '50': { name: 'Jake kim', age: 50 } },
      { '51': { name: 'John doe', age: 51 } },
      { '52': undefined },
      { '53': undefined },
      { '54': undefined }
    ]
about 4 years ago · Juan Pablo Isaza Relatório

0

I don't think you'll need the age data in your case. However I've provided the both versions which one use the age list but other does not. Please find them below.

const users = [{ "name": "John doe", "age": 51 }, { "name": "Jake kim", "age": 50 }, { "name": "Jim son", "age": 51 }];
const age = [50, 51, 52, 53, 54];

const desired = users.reduce((prev, cur) => {
  (prev[cur.age] ?? (prev[cur.age] = [])).push(cur); 
  return prev;
}, {});

console.log("This is the expected result", desired); 

const desired2 = users.reduce((prev, cur) => {
  (prev[cur.age] ?? (prev[cur.age] = [])).push(cur); 
  return prev;
}, age.reduce((prev, cur) => {
  prev[cur] = [];
  return prev;
}, {}));

console.log("This is the expected result with empty ages", desired2); 

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