mongoDB参数通过数组过滤
我通过带有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论