Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
How to get list of items with same id using find()

I'm getting data from my back-end the goal is when I want to click on the button of a specific id I want to get list of data with same id , because I need it to affect the data in charts NB : I couldn't change the form of data it should return like that cuz I'm using mongo db functions such as $group and $count

This is my data from back-end

Resolver [
  { _id: { _id: '123456789', totaloutcome: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_APPROVED_ONLINE' },
    count: 27
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_DECLINED' },
    count: 15
  }
]

for who want to see my back-end code I'm using NestJs

async getOutcomeCount() {
    return await this.tpeModel.aggregate([
      {
        $lookup: {
          from: 'logs',
          localField: 'terminalId',
          foreignField: 'terminalId',
          as: 'resultData',
        },
      },
      { $unwind: '$resultData' },

      {
        $group: {
          _id: {
            _id: '$resultData.terminalId',
            totaloutcome: '$resultData.outcome',
          },
          count: { $sum: 1 },
        },
      },
    ]);
  }

As I said I want to get list of data with _id:05000002 for e.g

Questions

  • Does I need to spread the object before making oprations of search or there is a solution to get what I need with this object to make all data (inner _id and count) in same object
    NB : Sorry for my bad english
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I'd recommend two things: first, a function that describes the value being looked for...

const innerId = obj => obj._id._id;

Then, a filter -- not a find -- to get elements of the array with that value...

const allWithId = id => resolverArray.filter(el => innerId(el) === id);

Demo...

let resolverArray = [
  { _id: { _id: '123456789', totaloutcome: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_APPROVED_ONLINE' },
    count: 27
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_DECLINED' },
    count: 15
  }
]

const innerId = obj => obj._id._id;

const allWithId = id => resolverArray.filter(el => innerId(el) === id);

console.log(allWithId('05000002'))

about 4 years ago · Juan Pablo Isaza Denunciar

0

The JavaScript function Array.find returns only the first match if any.

You should use Array.filter instead when there is more then one match, as per docs:

method creates a new array filled with elements that pass a test provided by a function

const matchingItems = yourArray.filter(item => item.id === 123)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda