我想要做的就是按DateEntered和Role分组并获取计数。 下面的代码可以工作,但是在完整日期的组到第二个。我需要按天分组(MM / DD / YYYY)
我也想使用Lambda表达式。
先谢谢你。
var group = new BsonDocument {
{ "_id", new BsonDocument
{
{ "DateEntered", "$DateEntered" },
{ "Role", "$Role" },
}
},
{ "Count", new BsonDocument("$sum", 1) }
};
var result = _collection.Aggregate()
.Group(group)
.ToListAsync().Result;
答案 0 :(得分:2)
我从这个例子中找到了答案 Mongodb aggregate not grouping documents by date
function lowerBound(arr, target) {
var first = 0,
count = arr.length;
while (count > 0) {
var step = count / 2;
var it = first + step;
if (arr[it] < target) {
first = it + 1;
count -= step + 1;
} else {
count = step;
}
}
return first;
}
function distanceSort(arr, target) {
var answer = [];
var j = lowerBound(arr, target);
var i = j-1;
while (i >= 0 || j<arr.length) {
if (j >= arr.length || target-arr[i]<arr[j]-target)
answer.push(arr[i--]);
else
answer.push(arr[j++]);
}
return answer;
}
console.log(distanceSort([-10,-6,3,5], 1)); //[3, 5, -6, -10]
console.log(distanceSort([-10,-6,3,5], -11)); //[-10, -6, 3, 5]
console.log(distanceSort([-10,-6,3,5], -10)); //[-10, -6, 3, 5]
console.log(distanceSort([-10,-6,3,5], 5)); //[5, 3, -6, -10]
console.log(distanceSort([-10,-6,3,5], 6)); //[5, 3, -6, -10]