如果字段数组和参数数组相交,则过滤MongoDb集合 [英] Filter MongoDb collection if field array and argument array intersect

查看:134
本文介绍了如果字段数组和参数数组相交,则过滤MongoDb集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个流星学习项目.其中有一个集合,其文档具有名为 keywords 的属性,该属性是字符串数组.我有第二个字符串数组.我想以这种方式过滤集合,它只返回 keywords 数组与第二个数组相交的那些文档,即两个数组都具有一个或几个相同的元素.有可能吗?

I'm creating a Meteor learning project. There is a collection in it, its documents have a property named keywords which is an array of strings. I have a second array of strings. I want to filter the collection that way, that it returned only those documents which keywords array intersect with that second array, i.e. both arrays have one or several same elements. Is it possible?

推荐答案

是的,查询将是:

var searchKeywords = ['a','b','c','d']

MyCollection = new Mongo.Collection('mycollection');

MyCollection.insert({
  keywords: ['x','y','a','b']
});
// returns some ID

MyCollection.findOne({
  keywords: { $in: searchKeywords } 
})._id
// returns the same ID

这篇关于如果字段数组和参数数组相交,则过滤MongoDb集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆