[
{
"_id": 5,
"name": "Wilburn Spiess",
"scores": [
{
"score": 44.87186330181261,
"type": "exam"
},
{
"score": 25.72395114668016,
"type": "quiz"
},
{
"score": 63.42288310628662,
"type": "homework"
}
]
},
{
"_id": 6,
"name": "Jenette Flanders",
"scores": [
{
"score": 37.32285459166097,
"type": "exam"
},
{
"score": 28.32634976913737,
"type": "quiz"
},
{
"score": 81.57115318686338,
"type": "homework"
}
]
},
{
"_id": 7,
"name": "Salena Olmos",
"scores": [
{
"score": 90.37826509157176,
"type": "exam"
},
{
"score": 42.48780666956811,
"type": "quiz"
},
{
"score": 96.52986171633331,
"type": "homework"
}
]
}
]
uj5u.com熱心網友回復:
查詢1
- 保留檔案,如果最低分數> 40(所有分數> 40)
玩蒙哥
aggregate(
[{"$match": {"$expr": {"$gt": [{"$min": "$scores.score"}, 40]}}}])
查詢2
>40和的所有分數<70- 并將陣列更改為將型別作為鍵作為您的預期輸出
玩蒙哥
aggregate(
[{"$match":
{"$expr":
{"$and":
[{"$gt": [{"$min": "$scores.score"}, 40]},
{"$lt": [{"$max": "$scores.score"}, 70]}]}}},
{"$set":
{"scores":
{"$arrayToObject":
[{"$map":
{"input": "$scores",
"as": "s",
"in": {"k": "$$s.type", "v": "$$s.score"}}}]}}},
{"$replaceRoot":
{"newRoot":
{"$mergeObjects": [{"name": "$name", "_id": "$_id"}, "$scores"]}}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/504148.html
標籤:mongodb mongodb查询 聚合框架 pymongo 嵌套文档
