$setIsSubset for regular queries in Mongo

时光总嘲笑我的痴心妄想 提交于 2020-08-25 07:11:51

问题


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].


回答1:


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" ] }    


来源:https://stackoverflow.com/questions/28819332/setissubset-for-regular-queries-in-mongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!