0
我有一个的MongoDB与以下形式的文件:格式化从MongoDB的/猫鼬组返回的对象由
{
...
"template" : "templates/Template1.html",
...
}
其中template
要么"templates/Template1.html"
,"templates/Template2.html"
或"templates/Template3.html"
。
我使用这个查询组由template
并指望有多少次,每次template
用于:
var group = {
key:{'template':1},
reduce: function(curr, result){ result.count++ },
initial: { count: 0 }
};
messageModel.collection.group(group.key, null, group.initial, group.reduce, null, true, cb);
我找回正确的结果,但它的格式如下:
{
"0" : {
"template" : "templates/Template1.html",
"count" : 2 },
"1" : {
"template" : "templates/Template2.html",
"count" : 2 },
"2" : {
"template" : "templates/Template3.html",
"count" : 1 }
}
我想知道是否有可能更改查询,以便它返回这样的:
{
"templates/Template1.html" : { "count" : 2 },
"templates/Template2.html" : { "count" : 2 },
"templates/Template3.html" : { "count" : 1 }
}
甚至:
{
"templates/Template1.html" : 2 ,
"templates/Template2.html" : 2 ,
"templates/Template3.html" : 1
}
我宁愿更改查询,而不是从原始查询解析返回的对象。
要清楚的是,从'.group()'和其他许多运算符返回的值实际上是对象的“数组”,而不是表示它的单个对象。使用“数据”作为“密钥”也是一种“反模式”,在可重复使用的代码中尤其是数据处理中应避免这种“反模式”。比较可靠的'.group()'选项更好的选择是诸如聚合框架之类的东西。但是同样的方式,输出最好是一个“光标”而不是一个单独的对象。所以让数据库成为数据库并且破坏你自己的结果。 –