Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python获得MongoDB中两个数组的交集的大小_Python_Arrays_Mongodb_Aggregation_Intersection - Fatal编程技术网

如何使用Python获得MongoDB中两个数组的交集的大小

如何使用Python获得MongoDB中两个数组的交集的大小,python,arrays,mongodb,aggregation,intersection,Python,Arrays,Mongodb,Aggregation,Intersection,我在MongoDB中存储了几个文档,每个文档都有一个twitter帐户id数组。我需要让mongoDB返回两个数组的交集的大小 文档如下所示: { 'screen_name': 'BBC', 'name': 'British Broadcasting Company', 'twitter_id' = '2139471834' 'followers' = [] # This array will likely have millions of elements }

我在MongoDB中存储了几个文档,每个文档都有一个twitter帐户id数组。我需要让mongoDB返回两个数组的交集的大小

文档如下所示:

{
    'screen_name': 'BBC',
    'name': 'British Broadcasting Company',
    'twitter_id' = '2139471834'
    'followers' = [] # This array will likely have millions of elements
}
{
    'screen_name': 'TheEconomist',
    'name': 'The Economist',
    'twitter_id' = '213945674'
    'followers' = [] # This array will likely have millions of elements
}
我希望聚合管道返回两个数组的交集的大小。我尝试过不同的方法返回交叉口的大小,但没有一种有效。我得到的最接近的查询是:

db.<collection>.aggregate(
[ 
  { $match : 
    { $or : 
      [ 
        {'screen_name': 'TheEconomist' }, 
        {'screen_name': 'BBC'} 
      ]
    }
  },
  { $project: 
    {'num_common_followers': 
      {$size: 
        { $setIntersection: 
          ['$followers']
        }
      }
    }
  }
])
db..aggregate(
[ 
{$match:
{$or:
[ 
{'screen_name':'TheEconomist'},
{'screen_name':'BBC'}
]
}
},
{$项目:
{'num_common_followers':
{$size:
{$setIntersection:
[“$followers”]
}
}
}
}
])
但是,这并不引用通过$match阶段的两个文档中的“followers”字段。因此,我似乎需要找到一种方法来引用每个文档中的“followers”字段,或者将带有两个数组的单个文档传递到$project阶段。我想要一个答案,但也有更好的方法做这个功能的任何建议。提前谢谢