我对mongodb加入两个集合和检索数据做了一个“小”查询。操纵MongoDB响应NodeJS
游戏:在URL上插入2个或3 PARAMS
-include可以是0,1或2
0独家 1以下 2返回所有
-netcode:是一键过滤数据 -group:另一个可选键,与第一个参数“包含”一起使用
- 我的查询完美地工作,返回某种方式事件发生的次数tain团体。
- 问题所在?我不能使用mongo db的结果,我需要将它解析为JSON。 我不是很聪明的JS,所以我不知道该把它放在哪里。由于我在公司工作,一些代码已经完成。
那么我的输出是这样的:
{
"events": [
{
"_id": {
"group": "GFS-CAJEROS-INFINITUM-TELDAT-M1",
"event": "SNMP DOWN"
},
"incidencias": 1
},
{
"_id": {
"group": "GFS-CAJEROS-MPLS",
"event": "Proactive Interface Input Utilisation"
},
"incidencias": 1209
},
{
"_id": {
"group": "GFS-CAJEROS-MPLS",
"event": "Proactive Interface Output Utilisation"
},
"incidencias": 1209
},
{
"_id": {
"group": "GFS-CAJEROS-MPLS",
"event": "Proactive Interface Availability"
},
"incidencias": 2199
},
{
"_id": {
"group": "GFS-SUCURSALES-HIBRIDAS",
"event": "Proactive Interface Output Utilisation"
},
"incidencias": 10
},
但我想它在一个JSON格式的融合,是这样的:检查int值是未来的事件的名称。
[
{
"group": "GFS-CAJEROS-MPLS",
"Proactive Interface Input Utilisation" : "1209",
"Proactive Interface Output Utilisation" : "1209",
"Proactive Interface Availability" : "2199",
},
{
"group": "GFS-SUCURSALES-HIBRIDAS",
"Proactive Interface Output Utilisation" : "10",
},
我使用和的NodeJS MongoDB的模块,因为我不知道这个功能完全相同的作品,我不知道如何管理响应,¿有一种更好的方式来做到这一点?喜欢得到json文件,用另一个js来生成它?
这是我使用的代码,基本上是重要的部分:
var events = db.collection('events');
events.aggregate([
{ $match : { netcode : data.params.netcode } },
{
$lookup:
{
from: "nodes",
localField: "name",
foreignField: "name",
as: "event_joined"
}
},
{ $unwind: {path: "$event_joined"} },
{ $match : {"event_joined.group" :
{$in:
[
groups[0] ,
groups[1] ,
groups[2] ,
groups[3] ,
groups[4] ,
groups[5] ,
groups[6] ,
groups[7] ,
groups[8] ,
groups[9] ,
]
}
}
},
{ $group : { _id : {group:"$event_joined.group", event:"$event"}, incidencias: { $sum: 1} } },
])
.toArray(function(err, result) {
if (err) {
console.log(err);
} else if (result) {
data.response.events = result;
} else {
console.log("No result");
}
不错的做法( - : – haim770