{ "_comment": [ "Inputs only. Expectations are measured -- see record.js.", "Expressions are exercised through $group, because that is where this", "server can reach them: $group's _id and its accumulator arguments are", "the only expression positions until $addFields and $project's computed", "fields land. A source that tested them anywhere else would be testing a", "stage that does not exist yet." ], "documents": [ { "_id": 1, "a": 10, "b": 3, "s": "x", "n": null, "f": false }, { "_id": 2, "a": 20, "b": 0, "s": "y", "f": true }, { "_id": 3, "a": -5, "b": 4, "s": "x", "n": 7, "f": false } ], "cases": [ { "description": "a compound _id groups on every field", "pipeline": [ { "$group": { "_id": { "s": "$s", "f": "$f" }, "n": { "$sum": 1 } } }, { "$sort": { "_id.s": 1, "_id.f": 1 } } ] }, { "description": "a nested compound _id", "pipeline": [ { "$group": { "_id": { "outer": { "inner": "$s" } }, "n": { "$sum": 1 } } }, { "$sort": { "_id.outer.inner": 1 } } ] }, { "description": "$literal keeps a $-string from being a path", "pipeline": [ { "$group": { "_id": { "$literal": "$s" }, "n": { "$sum": 1 } } } ] }, { "description": "$literal of a document", "pipeline": [ { "$group": { "_id": { "$literal": { "$sum": 1 } }, "n": { "$sum": 1 } } } ] }, { "description": "$add of a path and a constant", "pipeline": [ { "$group": { "_id": { "$add": [ "$a", 1 ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$subtract and $multiply", "pipeline": [ { "$group": { "_id": "$s", "d": { "$sum": { "$subtract": [ "$a", "$b" ] } }, "m": { "$sum": { "$multiply": [ "$a", 2 ] } } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$divide by zero is an error", "pipeline": [ { "$group": { "_id": null, "v": { "$sum": { "$divide": [ "$a", "$b" ] } } } } ] }, { "description": "$divide and $mod", "pipeline": [ { "$match": { "b": { "$gt": 0 } } }, { "$group": { "_id": "$s", "d": { "$push": { "$divide": [ "$a", "$b" ] } }, "m": { "$push": { "$mod": [ "$a", "$b" ] } } } }, { "$sort": { "_id": 1 } } ] }, { "description": "arithmetic over a missing field", "pipeline": [ { "$group": { "_id": { "$add": [ "$missing", 1 ] }, "n": { "$sum": 1 } } } ] }, { "description": "arithmetic over null", "pipeline": [ { "$group": { "_id": "$s", "v": { "$push": { "$add": [ "$n", 1 ] } } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$eq and $ne", "pipeline": [ { "$group": { "_id": { "$eq": [ "$s", "x" ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "comparison across types follows canonical order", "pipeline": [ { "$group": { "_id": { "$lt": [ "$a", "$s" ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$cmp returns the ordering", "pipeline": [ { "$group": { "_id": "$s", "v": { "$push": { "$cmp": [ "$a", "$b" ] } } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$and and $or are truthy over non-booleans", "pipeline": [ { "$group": { "_id": { "$and": [ "$a", "$f" ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$not of a missing field", "pipeline": [ { "$group": { "_id": { "$not": [ "$missing" ] }, "n": { "$sum": 1 } } } ] }, { "description": "$cond in its array form", "pipeline": [ { "$group": { "_id": { "$cond": [ { "$gt": [ "$a", 0 ] }, "positive", "negative" ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$cond in its document form", "pipeline": [ { "$group": { "_id": { "$cond": { "if": "$f", "then": "yes", "else": "no" } }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$ifNull falls through on missing and on null", "pipeline": [ { "$group": { "_id": { "$ifNull": [ "$n", "fallback" ] }, "count": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$switch picks the first matching branch", "pipeline": [ { "$group": { "_id": { "$switch": { "branches": [ { "case": { "$lt": [ "$a", 0 ] }, "then": "neg" }, { "case": { "$lt": [ "$a", 15 ] }, "then": "small" } ], "default": "big" } }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$switch with no branch matching and no default is an error", "pipeline": [ { "$group": { "_id": { "$switch": { "branches": [ { "case": false, "then": "never" } ] } }, "n": { "$sum": 1 } } } ] }, { "description": "an unknown expression operator is refused", "pipeline": [ { "$group": { "_id": { "$bogusExpr": "$a" }, "n": { "$sum": 1 } } } ] }, { "description": "two operators in one expression document are refused", "pipeline": [ { "$group": { "_id": { "$add": [ "$a", 1 ], "$literal": 2 }, "n": { "$sum": 1 } } } ] }, { "description": "a nested expression inside an accumulator argument", "pipeline": [ { "$group": { "_id": null, "v": { "$max": { "$add": [ { "$multiply": [ "$a", 2 ] }, "$b" ] } } } } ] }, { "description": "arithmetic over a non-numeric value is an error", "pipeline": [ { "$group": { "_id": { "$add": [ "$s", 1 ] }, "n": { "$sum": 1 } } } ] }, { "description": "$not takes a bare argument as well as an array", "pipeline": [ { "$group": { "_id": { "$not": "$f" }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$add of several operands", "pipeline": [ { "$group": { "_id": { "$add": [ "$a", "$b", 100 ] }, "n": { "$sum": 1 } } }, { "$sort": { "_id": 1 } } ] }, { "description": "$subtract needs exactly two operands", "pipeline": [ { "$group": { "_id": { "$subtract": [ "$a" ] }, "n": { "$sum": 1 } } } ] } ] }