你quesstion不是非常清晰,但似乎什么你想在这里做的是计数字段中的数据的发生,可选地通过匹配标准的值来过滤这些字段。
这里$cond
运营商允许您将logical condition等转换成数值:
db.collection.aggregate([
{ "$group": {
"_id": null,
"name": { "$sum": 1 },
"salary": {
"$sum": {
"$cond": [
{ "$gte": [ "$salary", 1000 ] },
1,
0
]
}
},
"type": {
"$sum": {
"$cond": [
{ "$eq": [ "$type", "type2" ] },
1,
0
]
}
}
}}
])
的所有值均在同一文件中,并没有真正使任何意义,他们在这里分手了,因为这是额外的正在筹备中。
{ "_id" : null, "name" : 3, "salary" : 3, "type" : 2 }
否则在长期的形式,这是不是很高性能由于需要使每个文档的副本,每一个键看起来像这样:
db.collection.aggregate([
{ "$project": {
"name": 1,
"salary": 1,
"type": 1,
"category": { "$literal": ["name","salary","type"] }
}},
{ "$unwind": "$category" },
{ "$group": {
"_id": "$category",
"count": {
"$sum": {
"$cond": [
{ "$and": [
{ "$eq": [ "$category", "name"] },
{ "$ifNull": [ "$name", false ] }
]},
1,
{ "$cond": [
{ "$and": [
{ "$eq": [ "$category", "salary" ] },
{ "$gte": [ "$salary", 1000 ] }
]},
1,
{ "$cond": [
{ "$and": [
{ "$eq": [ "$category", "type" ] },
{ "$eq": [ "$type", "type2" ] }
]},
1,
0
]}
]}
]
}
}
}}
])
而且它的输出:
{ "_id" : "type", "count" : 2 }
{ "_id" : "salary", "count" : 3 }
{ "_id" : "name", "count" : 3 }
如果您的文档没有统一的密钥名称,或者无法在管道条件中指定每个密钥,请使用mapReduce代替:
db.collection.mapReduce(
function() {
var doc = this;
delete doc._id;
Object.keys(this).forEach(function(key) {
var value = ((key == "salary") && (doc[key] < 1000))
? 0
: ((key == "type") && (doc[key] != "type2"))
? 0
: 1;
emit(key,value);
});
},
function(key,values) {
return Array.sum(values);
},
{
"out": { "inline": 1 }
}
);
而且它的输出:
"results" : [
{
"_id" : "name",
"value" : 3
},
{
"_id" : "salary",
"value" : 3
},
{
"_id" : "type",
"value" : 2
}
]
这基本上是一个有条件的计数同样的事情,但你只指定了“反向”的状况下,你想,只为要到田间地头过滤条件。当然,这种输出格式很容易作为单独的文档发布。
同样的方法适用于在条件满足的条件下测试满足条件的条件并返回1
满足条件的条件或0
不适用于总计计数的条件。
你会更好地展示你目前在做什么。你的解释目前还不清楚,因为你的“参数”与你所要的输出的关系。 –
@布莱克斯,我的标准将是或条件(例如薪水> 10000,姓名= xxx)在这个标准中,我需要得到通过这个标准的工资数和曾经有过相同名字的人数。 –