流星.js按字段中的值筛选集合

Meteor.js filter collections by value in field

本文关键字:筛选 集合 js 字段 流星      更新时间:2023-09-26

目前在meteor中.js我试图弄清楚如何返回具有包含特定值的字段的所有集合:

Posts.insert({
  tags: ['test', 'test1', 'test2'],
  categories: ['test', 'planning'],
  article: {
    title: 'Lorem ipsum dollar sum import',
    content: 'Lorem balck solo su, bella hun sillo.',
    author: 'test'
  },
  comments: [{title: 'test', 'content': 'hello world'},],
});

因此,例如,在按类别过滤帖子的上下文中,我如何返回包含与"test"或"计划"匹配的字符串的类别数组的所有集合,并排除数组中不包含该字符串的集合?

这是针对我一直在开发的一个简单的博客应用程序,如果有更有效的方法来存储帖子和相关信息,请让我知道世界很棒。

您可以使用

$in运算符。例如:

Posts.find({categories: {$in: ['test', 'planning']}});

将找到categories至少有一个元素与'test''planning'匹配的所有帖子。