Mongodb 使用python高效地将mongo find()中的字段值转换为整数

Mongodb 使用python高效地将mongo find()中的字段值转换为整数,mongodb,python-2.7,pymongo,pymongo-2.x,Mongodb,Python 2.7,Pymongo,Pymongo 2.x,给定下面这样的mongo文档,如何高效地查找所有文档并将student_id字段返回为整数 { "_id" : ObjectId("58dd757910d81946b8ff853a"), "student_id": "4169506398", "first_name": "steven", "last_name": "smith", "date_of_birth": "02-07-1988" } { "_id" : ObjectId(

给定下面这样的mongo文档,如何高效地查找所有文档并将student_id字段返回为整数

 {
     "_id" : ObjectId("58dd757910d81946b8ff853a"),
     "student_id": "4169506398",
     "first_name": "steven",
     "last_name": "smith",
     "date_of_birth": "02-07-1988"
}
{
     "_id" : ObjectId("58dd757910d81946b8ff853b"),
     "student_id": "6902",
     "first_name": "michael",
     "last_name": "clarke",
     "date_of_birth": "05-30-1988"
}
预期结果(Json):

{
     "student_id": 4169506398
}
{
     "student_id": 6902
}

我有29000张唱片。使用db.find({})获取文档并在循环时将student_id强制转换为整数可能会出现性能问题

如果没有客户端处理或map reduce,您就无法做到这一点

mylist = map(int, db.collection.distinct('student_id'))
它在PY2中生成类型为
int
student\u id
列表,或在PY3中生成迭代器对象列表

如果“student\u id”在集合中不是唯一的,并且您不想对结果进行重复数据消除,也可以使用所示的
.aggregate()
方法