MongoDB新手在这里...
当我在 shell 中执行 db.students.find().pretty() 时,我会从我的收藏中得到一个很长的列表......就像这样......
{
"_id" : 19,
"name" : "Gisela Levin",
"scores" : [
{
"type" : "exam",
"score" : 44.51211101958831
},
{
"type" : "quiz",
"score" : 0.6578497966368002
},
{
"type" : "homework",
"score" : 93.36341655949683
},
{
"type" : "homework",
"score" : 49.43132782777443
}
]
}
现在我有大约 100 多个...我需要在每个上面运行以下命令...
lowest_hw_score =
db.students.aggregate(
// Initial document match (uses index, if a suitable one is available)
{ $match: {
_id : 0
}},
// Expand the scores array into a stream of documents
{ $unwind: '$scores' },
// Filter to 'homework' scores
{ $match: {
'scores.type': 'homework'
}},
// Sort in descending order
{ $sort: {
'scores.score': 1
}},
{ $limit: 1}
)
所以我可以在每个结果上运行这样的东西
for item in lowest_hw_score:
print lowest_hw_score
现在“lowest_score”只适用于一个项目我要在集合中的所有项目上运行它......我该怎么做?