{ "_comment": [ "Inputs only. Expectations are measured -- see record.js.", "Every pipeline ends in a $sort, because $group's output order is", "unspecified and a corpus that depended on it would fail for the wrong", "reason on either server." ], "documents": [ { "_id": 1, "g": "a", "x": 10, "s": "p", "t": [1, 2] }, { "_id": 2, "g": "a", "x": 20, "s": "q", "t": [2, 3] }, { "_id": 3, "g": "b", "x": 30, "s": "p", "t": [] }, { "_id": 4, "g": "b", "x": 7, "s": "r" }, { "_id": 5, "g": "b", "x": "not a number", "s": "p", "t": [4] } ], "cases": [ { "description": "$sum over a field path", "pipeline": [{ "$group": { "_id": "$g", "v": { "$sum": "$x" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$sum counts with a constant", "pipeline": [{ "$group": { "_id": "$g", "v": { "$sum": 1 } } }, { "$sort": { "_id": 1 } }] }, { "description": "$avg ignores non-numeric values", "pipeline": [{ "$group": { "_id": "$g", "v": { "$avg": "$x" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$avg of a group with no numeric value at all", "pipeline": [{ "$group": { "_id": "$s", "v": { "$avg": "$missing" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$min and $max compare across types", "pipeline": [{ "$group": { "_id": "$g", "lo": { "$min": "$x" }, "hi": { "$max": "$x" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$min of a field no document has", "pipeline": [{ "$group": { "_id": "$g", "v": { "$min": "$missing" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$first and $last follow the input order", "pipeline": [{ "$sort": { "_id": 1 } }, { "$group": { "_id": "$g", "f": { "$first": "$x" }, "l": { "$last": "$x" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$push keeps duplicates and order", "pipeline": [{ "$sort": { "_id": 1 } }, { "$group": { "_id": "$s", "v": { "$push": "$g" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$push of a missing field skips it", "pipeline": [{ "$sort": { "_id": 1 } }, { "$group": { "_id": "$g", "v": { "$push": "$t" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$addToSet drops duplicates", "pipeline": [{ "$group": { "_id": "$s", "v": { "$addToSet": "$g" } } }, { "$sort": { "_id": 1 } }] }, { "description": "$count is the number of documents in the group", "pipeline": [{ "$group": { "_id": "$g", "v": { "$count": {} } } }, { "$sort": { "_id": 1 } }] }, { "description": "a compound _id groups on every field", "pipeline": [{ "$group": { "_id": { "g": "$g", "s": "$s" }, "n": { "$sum": 1 } } }, { "$sort": { "_id.g": 1, "_id.s": 1 } }] }, { "description": "a constant _id puts everything in one group", "pipeline": [{ "$group": { "_id": null, "n": { "$sum": 1 } } }] }, { "description": "an _id path that no document has", "pipeline": [{ "$group": { "_id": "$missing", "n": { "$sum": 1 } } }] }, { "description": "grouping on an array field", "pipeline": [{ "$group": { "_id": "$t", "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } }] }, { "description": "an unknown accumulator is refused", "pipeline": [{ "$group": { "_id": "$g", "v": { "$bogusAcc": "$x" } } }] }, { "description": "an accumulator that is not a document is refused", "pipeline": [{ "$group": { "_id": "$g", "v": "$x" } }] }, { "description": "two accumulators in one field are refused", "pipeline": [{ "$group": { "_id": "$g", "v": { "$sum": "$x", "$max": "$x" } } }] }, { "description": "a $group without _id is refused", "pipeline": [{ "$group": { "n": { "$sum": 1 } } }] } ] }