以下是我在mongodb集合中獲取的一組樣本數據的示例。在mongodb中檢索數據作爲嵌套文檔
{year : 2010 , studentName: "John", grades :{science: 70, maths: 80, english: 85 }}
{year : 2010 , studentName: "Denver", grades :{science: 75, maths: 85, english: 85 }}
{year : 2010 , studentName: "Harry", grades :{science: 85, maths: 75, english: 65 }}
{year : 2011 , studentName: "John", grades :{science: 70, maths: 80, english: 85 }}
{year : 2011 , studentName: "Denver", grades :{science: 75, maths: 85, english: 85 }}
{year : 2011 , studentName: "Harry", grades :{science: 85, maths: 75, english: 65 }}
我想用下面的模式檢索嵌套文檔的數據。
[{
"year": 2010,
"studentGrades": [
{
"studentName": "John",
"grades": {
"science": 70,
"maths": 80,
"english": 85
}
},
{
"studentName": "Denver",
"grades": {
"science": 75,
"maths": 85,
"english": 85
}
}
]
},
{
"year": 2011,
"studentGrades": [
{
"studentName": "John",
"grades": {
"science": 70,
"maths": 80,
"english": 85
}
},
{
"studentName": "Denver",
"grades": {
"science": 75,
"maths": 85,
"english": 85
}
}
]
}
]
你能幫我寫出mongodb query命令來獲取日期範圍內的數據,例如: year
之間2009 -2011
與上述格式的結果?
這看起來像一個簡單的[聚集](https://docs.mongodb.org/manual/aggregation/)與[ $基](https://docs.mongodb.org/manual/reference/operator/aggregation/group/)。 – Philipp