我的函数如下所示。如何在MongoDB中进行聚合操作时限制输出中的列数
function (x)
{
var SO2Min = db.AirPollution.aggregate(
[
{
$match : {"SO2":{$ne:'NA'}, "State":{$eq: x} }
},
{
$group:
{
_id: x,
SO2MinQuantity: { $min: "$SO2" }
}
},
{
$project:
{SO2MinQuantity: '$SO2MinQuantity'
}
}
]
)
db.AirPollution.update
(
{
"State": "West Bengal"},
{
$set: {
"MaxSO2": SO2Max
}
},
{
"multi": true
}
);
}
在这里,AirPolltuion是我的收藏。如果我运行这个函数,那么集合将被更新为新列MaxSO2,如下所示。
{
"_id" : ObjectId("5860a2237796484df5656e0c"),
"Stn Code" : 11,
"Sampling Date" : "02/01/15",
"State" : "West Bengal",
"City/Town/Village/Area" : "Howrah",
"Location of Monitoring Station" : "Bator, Howrah",
"Agency" : "West Bengal State Pollution Control Board",
"Type of Location" : "Residential, Rural and other Areas",
"SO2" : 10,
"NO2" : 40,
"RSPM/PM10" : 138,
"PM 2.5" : 83,
"MaxSO2" : {
"_batch" : [
{
"_id" : "West Bengal",
"SO2MaxQuantity" : 153
}
],
"_cursor" : {}
}
}
我们可以看到,MaxSO2已被添加为子文档。但是我希望将该列添加到同一个文档中作为一个字段,而不是作为子文档的一部分。准确地说,我不希望批处理和光标字段出现。请帮忙。
为了澄清,我想改变的文件是这样的: –
为了澄清,我想改变的文件是这样的:{ “_id”:物件(“5860a2237796484df5656e0c”), “状态”: “西孟加拉邦”, “SO2”:10, “NO2”:40, “RSPM/PM10”:138, “PM 2.5”:83, “SO2MaxQuantity”:153 } –