MongoDB parameters to filter through array

55 Views Asked by At

I have a problem through an array with mongoDB parameters... assuming I have this two arrays

const diversityArray = ['video', 'gps']
const entries = [
  {
    name: 'First entry',
    slug: 'first',
    diversity: 'image, video'
  },
  {
    name: 'Second entry',
    slug: 'second',
    diversity: 'image, gps'
  },
  {
    name: 'Third entry',
    slug: 'third',
    diversity: 'iframe'
  }];

How could I do this but with mongoDB parameters? I want to filter all the items in the array that contains one or more of the items on the diversityArray.

const filtered = entries.filter(item => diversityArray.some(data => item.diversity.includes(data)));

I need to do this because I have a larger and more complex array coming from Builder.io Headless CMS and I want to do the filter in the query.

I have this code right now:

    const sets = await builder.getAll('open-dataset', {
      options: { noTargeting: true },
      limit: 100,
      query: {
        data: {
          date: { $gt: newMinYear, $lt: newMaxYear },
          title: { $regex: search, $options: 'i' },
          diversity: ...what I want to do
        }
      }
    });

Thanks!

0

There are 0 best solutions below