以下是MongoDB数据库中的文档之一。
我想在2007年,2008年之间选择年度。
并且密钥包括“实际”或“范围的上端”
并且table_name等于失业率
如何在Mongoid或MongoDB查询中完成它。
或者我只能在Ruby或Python这样的应用程序层中完成它?
{
"_id": "2012-04-25_unemployment_rate",
"table_name": "Unemployment rate",
"unit": "Percent",
"data": [
{
"2007": [
{
"Actual": "3.5"
},
{
"Upper End of Range": "-"
},
{
"Upper End of Central Tendency": "-"
},
{
"Lower End of Central Tendency": "-"
},
{
"Lower End of Range": "-"
}
]
},
{
"2008": [
{
"Actual": "1.7"
},
{
"Upper End of Range": "-"
},
{
"Upper End of Central Tendency": "-"
},
{
"Lower End of Central Tendency": "-"
},
{
"Lower End of Range": "-"
}
]
}
}
{
"_id": "2014-04-25_unemployment_rate",
"table_name": "Unemployment rate",
"unit": "Percent",
"data": [
{
"2008": [
{
"Actual": "3.5"
},
{
"Upper End of Range": "-"
},
{
"Upper End of Central Tendency": "-"
},
{
"Lower End of Central Tendency": "-"
},
{
"Lower End of Range": "-"
}
]
},
{
"2009": [
{
"Actual": "1.7"
},
{
"Upper End of Range": "-"
},
{
"Upper End of Central Tendency": "-"
},
{
"Lower End of Central Tendency": "-"
},
{
"Lower End of Range": "-"
}
]
}
}
答案 0 :(得分:1)
您不按键选择文件;您可以按值选择文档。您应该重新构建文档以包含"year" : 2007
等字段。例如,
{
"_id": "2012-04-25_unemployment_rate",
"table_name": "Unemployment rate",
"unit": "Percent",
"data": [
{
"year" : 2007,
"Actual": "3.5",
"Upper End of Range": "-",
"Upper End of Central Tendency": "-",
"Lower End of Central Tendency": "-",
"Lower End of Range": "-"
}
]
}
我不确定“键包含'实际'或'范围的上端'”的条件是什么意思,但是如果您希望文档的数据元素具有year
2007或2008和{ {1}}等于table_name
,请使用查询规范
"Unemployment rate"