从 Python mongodb 中的列表中计算特定值
Count a particular value from list in Python mongodb
我正在试验 Python 和 MongoDB。我是 python 的新手。在这里,我从一个集合中获取记录,并根据该集合中的特定值,找到该记录的计数(来自第一个集合)。但我的问题是我无法将此计数附加到我的列表中。
代码如下:
@gen.coroutine
def post(self):
Sid = self.body['Sid']
alpha = []
test = db.student.find({"Sid": Sid})
count = yield test.count()
print(count)
for document in (yield test.to_list(length=1000)):
cursor = db.attendance.find({"StudentId": document.get('_id')})
check = yield cursor.count()
print(check)
alpha.append(document)
self.write(bson.json_util.dumps({"data": alpha}))
显示的输出alpha来自第一个集合(student),count值来自(attendance集合)
当我尝试通过检查扩展列表时,我以错误告终
alpha.append(document.extend(check))
但是我在 python 终端中得到了正确的计数值,我无法将它与输出一起写入。
我的输出就像
{"data": [{"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."}}, {"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."}}]}
我的输出应该是这样的
{"data": [{"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."},"count": "5"}, {"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."},"count": "3"}]}
请指导我如何获得我想要的输出。
谢谢。
将 count
条目添加到字典 document
并追加字典:
for document in (yield test.to_list(length=1000)):
cursor = db.attendance.find({"StudentId": document.get('_id')})
check = yield cursor.count()
document['count'] = check
alpha.append(document)
更好的方法是使用您正在使用的 python 驱动程序中的 MongoDB .aggregate()
方法,而不是重复使用 .find()
和 .count()
操作:
db.attendance.aggregate([
{ "$group": {
"_id": "$StudentId",
"name": { "$first": "$Student Name" },
"count": { "$sum": 1 }
}}
])
那么已经帮你搞定了
您当前的代码所做的是查找当前学生并return计算 "count" 出现的次数。你正在通过输出的内容为每个学生做这件事。
而不是这样做,数据是 "aggregated" 到 return 文档中的值以及 returned 结果中的 "count",它是每个学生合计。
这意味着您不需要 运行 查询每个学生就可以得到计数。相反,您只需调用数据库 "once" 并让它在一个结果中计算出您需要的所有学生。
如果您需要一个学生而不是所有学生,那么您可以使用查询条件对其进行过滤;
db.attendance.aggregate([
{ "$match": { "StudentId": { "$in": list_of_student_ids } } },
{ "$group": {
"_id": "$StudentId",
"name": { "$first": "$Student Name" },
"count": { "$sum": 1 }
}}
])
并为您完成选择和聚合。
无需循环代码和大量数据库请求。 .aggregate()
方法和管道将为您完成。
阅读 Aggregation Pipeline 上的核心文档。
我正在试验 Python 和 MongoDB。我是 python 的新手。在这里,我从一个集合中获取记录,并根据该集合中的特定值,找到该记录的计数(来自第一个集合)。但我的问题是我无法将此计数附加到我的列表中。
代码如下:
@gen.coroutine
def post(self):
Sid = self.body['Sid']
alpha = []
test = db.student.find({"Sid": Sid})
count = yield test.count()
print(count)
for document in (yield test.to_list(length=1000)):
cursor = db.attendance.find({"StudentId": document.get('_id')})
check = yield cursor.count()
print(check)
alpha.append(document)
self.write(bson.json_util.dumps({"data": alpha}))
显示的输出alpha来自第一个集合(student),count值来自(attendance集合)
当我尝试通过检查扩展列表时,我以错误告终
alpha.append(document.extend(check))
但是我在 python 终端中得到了正确的计数值,我无法将它与输出一起写入。
我的输出就像
{"data": [{"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."}}, {"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."}}]}
我的输出应该是这样的
{"data": [{"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."},"count": "5"}, {"Sid": "1", "Student Name": "Alex","_id": {"$oid": "..."},"count": "3"}]}
请指导我如何获得我想要的输出。
谢谢。
将 count
条目添加到字典 document
并追加字典:
for document in (yield test.to_list(length=1000)):
cursor = db.attendance.find({"StudentId": document.get('_id')})
check = yield cursor.count()
document['count'] = check
alpha.append(document)
更好的方法是使用您正在使用的 python 驱动程序中的 MongoDB .aggregate()
方法,而不是重复使用 .find()
和 .count()
操作:
db.attendance.aggregate([
{ "$group": {
"_id": "$StudentId",
"name": { "$first": "$Student Name" },
"count": { "$sum": 1 }
}}
])
那么已经帮你搞定了
您当前的代码所做的是查找当前学生并return计算 "count" 出现的次数。你正在通过输出的内容为每个学生做这件事。
而不是这样做,数据是 "aggregated" 到 return 文档中的值以及 returned 结果中的 "count",它是每个学生合计。
这意味着您不需要 运行 查询每个学生就可以得到计数。相反,您只需调用数据库 "once" 并让它在一个结果中计算出您需要的所有学生。
如果您需要一个学生而不是所有学生,那么您可以使用查询条件对其进行过滤;
db.attendance.aggregate([
{ "$match": { "StudentId": { "$in": list_of_student_ids } } },
{ "$group": {
"_id": "$StudentId",
"name": { "$first": "$Student Name" },
"count": { "$sum": 1 }
}}
])
并为您完成选择和聚合。
无需循环代码和大量数据库请求。 .aggregate()
方法和管道将为您完成。
阅读 Aggregation Pipeline 上的核心文档。