本文介绍了$setIsSubset 用于 Mongo 中的常规查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找相当于 $setIsSubset http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/ 用于 MongoDB 中的常规(即非聚合)查询.我该怎么做?

假设我有文件

{ 'x' : ['A', 'B'] }{'x':['A','D']}

还有那个

filter = ['A', 'B', C']

我想做一个find({"x" : {'$setIsSubSet':filter}}) 并期望返回

{ 'x' : ['A', 'B'] }

似乎大多数条件命令匹配any而不是all.我也希望它是一个子集,所以看起来$and并且 $all 不会将 [A,B] 匹配到 [A,B,C].

解决方案

您可以在 shell 中尝试以下操作:

var filer = ['A', 'B', 'C']db.coll2.find({x: {"$not": {"$elemMatch": {"$nin" : filer }}}})

输出{ "_id" : ObjectId("54f4d72f1f22d4a529052760"), "x" : [ "A", "B" ] }

I am looking to do the equivalent of $setIsSubset http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/ for regular (i.e. NOT aggregate) queries in MongoDB. How can I do this?

Assume that I have the documents

{ 'x' : ['A', 'B'] }
{ 'x' : ['A', 'D'] }

And that

filter = ['A', 'B', C']

I want to do a find({"x" : {'$setIsSubSet':filter}}) and expect only to get back

{ 'x' : ['A', 'B'] }

It seems like most conditional commands match any not all. I also want it to be a subset, so it seems that $and and $all would not match [A,B] to [A,B,C].

解决方案

You could try the following in the shell:

var filer = ['A', 'B', 'C']
db.coll2.find({x: {"$not": {"$elemMatch": {"$nin" : filer }}}})

Output

    { "_id" : ObjectId("54f4d72f1f22d4a529052760"), "x" : [  "A",  "B" ] }    

这篇关于$setIsSubset 用于 Mongo 中的常规查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:16