以下是Mongo文档的集合,我正尝试为其计算中位数:
[
{
"_id" : ObjectId(“123”),
"time" : NumberLong(2110)
},
{
"_id" : ObjectId(“1234”),
"time" : NumberLong(3110)
},
{
"_id" : ObjectId(“1235”),
"time" : NumberLong(5110)
}
]
代码如下:
Cursor1 = collection.find({“_id” : {$in: [ObjectId(“123”), ObjectId(“1234”),
ObjectId(“1235”)]}},{“_id":False, "time":True})
timeTaken = cursor1['time']
print(time)
median = statistics.median(time)
print(median)
我看到此错误:
TypeError:字符串索引必须是整数,而不是str