我需要使用PyMongo构建一个查询,它从MongoDB数据库中的两个相关集合中获取数据。
集合X包含字段UserId,Name和EmailId:
[
{
"UserId" : "941AB",
"Name" : "Alex Andresson",
"EmailId" : "alex@example.com"
},
{
"UserId" : "768CD",
"Name" : "Bryan Barnes",
"EmailId" : "bryan@example.com"
}
]
集合Y包含字段UserId1,UserID2和Rating:
[
{
"UserId1" : "941AB",
"UserId2" : "768CD",
"Rating" : 0.8
}
]
我需要打印UserId1和UserId2的名称和电子邮件ID以及评级,如下所示:
[
{
"UserId1" : "941AB",
"UserName1" : "Alex Andresson"
"UserEmail1" : "alex@example.com",
"UserId2" : "768CD",
"UserName2" : "Bryan Barnes"
"UserEmail2" : "bryan@example.com",
"Rating": 0.8
}
]
这意味着我需要从集合Y和X中获取数据。我现在正在与PyMongo合作,但我找不到它的解决方案。有人甚至可以给我一个关于这个概念或方法的伪代码如何推进它。
答案 0 :(得分:0)
您需要手动加入或使用一些可以为您完成此操作的库 - 也许mongoengine。
基本上,您需要找到您感兴趣的评分,然后找到与这些评分相关的用户。
示例:
#!/usr/bin/env python3
import pymongo
from random import randrange
client = pymongo.MongoClient()
db = client['test']
# clean collections
db['users'].drop()
db['ratings'].drop()
# insert data
user_count = 100
rating_count = 20
db['users'].insert_many([
{'UserId': i, 'Name': 'John', 'EmailId': i}
for i in range(user_count)])
db['ratings'].insert_many([
{'UserId1': randrange(user_count), 'UserId2': randrange(user_count), 'Rating': i}
for i in range(rating_count)])
# don't forget the indexes
db['users'].create_index('UserId')
# but it would be better if we used _id as the UserId
# if you want to make queries based on Rating value, then add also this index:
db['ratings'].create_index('Rating')
# now print ratings with users that have value 10+
# simple approach:
ratings = db['ratings'].find({'Rating': {'$gte': 10}})
for rating in ratings:
u1 = db['users'].find_one({'UserId': rating['UserId1']})
u2 = db['users'].find_one({'UserId': rating['UserId2']})
print('Rating between {} (UserId {:2}) and {} (UserId {:2}) is {:2}'.format(
u1['Name'], u1['UserId'], u2['Name'], u2['UserId'], rating['Rating']))
print('---')
# optimized approach:
ratings = list(db['ratings'].find({'Rating': {'$gte': 10}}))
user_ids = {r['UserId1'] for r in ratings}
user_ids |= {r['UserId2'] for r in ratings}
users = db['users'].find({'UserId': {'$in': list(user_ids)}})
users_by_id = {u['UserId']: u for u in users}
for rating in ratings:
u1 = users_by_id.get(rating['UserId1'])
u2 = users_by_id.get(rating['UserId2'])
print('Rating between {} (UserId {:2}) and {} (UserId {:2}) is {:2}'.format(
u1['Name'], u1['UserId'], u2['Name'], u2['UserId'], rating['Rating']))
请注意,第一种方法为评级调用一个find
,每个评级调用两个find
,但第二种方法总共调用三个find
。如果您通过网络访问MongoDB,这将导致巨大的性能差异。
如果可能,我建议使用_id
代替UserId
。
当然,使用SQL数据库时,这个特定的用例会更容易。如果您使用MongoDB获得性能并且读取的内容比写入多得多,那么请考虑将相关用户缓存到评级文档中。