Home > Software design >  How to get list of items with same id using find()
How to get list of items with same id using find()

Time:05-18

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

CodePudding user response:

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'))

CodePudding user response:

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)
  • Related