Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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 以一个字段为基础计算另一个字段_Python_Mongodb - Fatal编程技术网

Python 以一个字段为基础计算另一个字段

Python 以一个字段为基础计算另一个字段,python,mongodb,Python,Mongodb,我是MongoDB的初学者。我收集了几百万份文件。文件示例: 我想找到每个唯一用户id的用户\追随者\数量、用户\朋友\数量和用户\提及总数 data.aggregate([ {"$group" : {"_id":{"followers_count":"$user_followers_count", "friends_count": "$user_friends_coun

我是MongoDB的初学者。我收集了几百万份文件。文件示例:

我想找到每个唯一用户id的用户\追随者\数量、用户\朋友\数量和用户\提及总数

data.aggregate([
       {"$group" : {"_id":{"followers_count":"$user_followers_count", "friends_count": "$user_friends_count"}, "followers_count":{"$sum:1"}}} ])

我正在尝试,但没有得到结果。有人能帮忙吗?

这可以为您提供预期的输出:

db.data.aggregate([ {$project:{usr_mentions:{ $cond: { if: { $isArray: "$user_mentions" }, then: { $size: "$user_mentions" }, else: 0} } , user_id:1,user_followers_count:1,user_friends_count:1   }}   ,  {$group:{ _id:"$user_id" , user_followers_total_count:{$sum:"$user_followers_count"} , user_friends_total_count:{$sum:"$user_friends_count"} , usr_mentions_total_count:{ $sum:"$usr_mentions" }     }}    ])
输出如下所示:

{ "_id" : userX, "user_followers_total_count" : 50, "user_friends_total_count" : 20, "usr_mentions_total_count" : 2 }
{ "_id" : userY, "user_followers_total_count" : 150, "user_friends_total_count" : 60, "usr_mentions_total_count" : 6 }
请记住,如果在项目阶段没有提及次数,查询将只需要分组阶段,而且会更快

mongod/mongos 4.4测试:

mongos> db.data.find()
{ "_id" : ObjectId("5ff4f4e6df14d22947f36205"), "tweet_id" : 1, 
"user_id" : 2, "user_followers_count" : 50, "user_friends_count" : 20, 
"user_tweets_count" : 30, "user_mentions" : [ 0, 1 ] }
{ "_id" : ObjectId("5ff4f4f4df14d22947f36206"), "tweet_id" : 3, 
"user_id" : 4, "user_followers_count" : 50, "user_friends_count" : 20, 
"user_tweets_count" : 30, "user_mentions" : [ 0, 1 ] }
{ "_id" : ObjectId("5ff4f58bdf14d22947f36207"), "tweet_id" : 3, 
"user_id" : 4, "user_followers_count" : 50, "user_friends_count" : 20, 
"user_tweets_count" : 30, "user_mentions" : [ 0, 1 ] }
{ "_id" : ObjectId("5ff4f590df14d22947f36208"), "tweet_id" : 3, 
"user_id" : 4, "user_followers_count" : 50, "user_friends_count" : 20, 
"user_tweets_count" : 30, "user_mentions" : [ 0, 1 ] }

mongos> db.data.aggregate([ {$project:{usr_mentions:{ $cond: { if: { 
$isArray: "$user_mentions" }, then: { $size: "$user_mentions" }, else: 
 0} } , user_id:1,user_followers_count:1,user_friends_count:1   }}   ,  
{$group:{ _id:"$user_id" , user_followers_total_count: 
{$sum:"$user_followers_count"} , user_friends_total_count: 
{$sum:"$user_friends_count"} , usr_mentions_total_count:{ 
$sum:"$usr_mentions" }     }}    ])

{ "_id" : 4, "user_followers_total_count" : 150, 
"user_friends_total_count" : 60, "usr_mentions_total_count" : 6 }
{ "_id" : 2, "user_followers_total_count" : 50, 
"user_friends_total_count" : 20, "usr_mentions_total_count" : 2 }
mongos> 

谢谢你的回复。这给了我一个无效的语法错误。(用版本4.4测试)抱歉。我在Pymongo语法中犯了一些错误。成功了。谢谢!