mongoDB参数通过数组过滤

发布于 2025-01-22 14:35:42 字数 1020 浏览 0 评论 0原文

我通过带有mongoDB参数的数组有一个问题...假设我有这两个数组,

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'
  }];

我该怎么做,但是使用mongoDB参数?我想过滤阵列中包含一个或多个多样性array中一个或多个项目的所有项目。

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

我需要这样做,因为我有一个来自Builder.io无头CM的更大,更复杂的数组,我想在查询中执行过滤器。

我现在有这个代码:

    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
        }
      }
    });

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文