我如何只显示table_name
和data
字段2013
,2014
记录包含许多字段,
_id
,table_name
,unit
,...,schedule_id
我怎么能只显示table_name
和data
字段2013
,2014
,我想知道如何使用Pymongo,mongoDB和Mongoid
更具体地说,我希望记录以下列格式返回。
{
"_id": "2012-04-25_pce_inflation",
"table_name": "pce_inflation",
"unit": "Percent",
"data": {
"2012": {
"number_of_participants": "3"
},
"2013": {
"number_of_participants": "3"
},
"2014": {
"number_of_participants": "7"
},
"2015": {
"number_of_participants": "4"
}
},
"end_date": new Date("2012-04-25T08:00:00+0800"),
"updated_at": new Date(1426741272196),
"created_at": new Date(1426741272195),
"schedule_id": "2012-04-25"
}
{
"table_name": "pce_inflation",
"data": {
"2013": {
"number_of_participants": "3"
},
"2014": {
"number_of_participants": "7"
}
},
}
答案 0 :(得分:1)
您需要的是projection
这是一般方法,而不是特定于驱动程序的解决方案。
例如,您在mongo shell中的常规查询为:
db.coll.find({"table_name": "pce_inflation"})
现在,您想摆脱_id
字段,您的查询将变为:
db.coll.find({"table_name": "pce_inflation"}, {"_id":0 , "table_name":1, "data.2013":1, "data.2014":1})
投影的语法在上面的链接中指定
<字段>:< 1或true>指定包含字段。
<字段>:< 0或false>指定字段的抑制。