Files
mongo/jstests/aggregation/sources/count.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

47 lines
1.4 KiB
JavaScript

// @tags: [
// requires_fcv_81,
// ]
import {assertErrCodeAndErrMsgContains, assertErrorCode} from "jstests/aggregation/extras/utils.js";
const coll = db.aggregation_count;
coll.drop();
{
const pipeline = [{"$count": "_id"}];
assertErrorCode(coll, pipeline, 9039800);
assertErrCodeAndErrMsgContains(coll, pipeline, 9039800, "the count field cannot be '_id'");
}
// Tests invalid specs for $count.
assertErrorCode(coll, [{"$count": 1}], 40156);
assertErrorCode(coll, [{"$count": ""}], 40157);
assertErrorCode(coll, [{"$count": "$x"}], 40158);
assertErrorCode(coll, [{"$count": "te\u0000st"}], 40159);
assertErrorCode(coll, [{"$count": "test.string"}], 40160);
assert.commandWorked(coll.insertMany([...Array(1000).keys()].map((i) => ({a: i, condition: i % 2}))));
{
const pipeline = [{"$count": "test"}];
const result = coll.aggregate(pipeline).toArray();
assert.eq([{"test": 1000}], result);
}
{
const pipeline = [{"$count": "myCount"}];
const result = coll.aggregate(pipeline).toArray();
assert.eq([{"myCount": 1000}], result);
}
{
const pipeline = [{"$count": "quantity"}];
const result = coll.aggregate(pipeline).toArray();
assert.eq([{"quantity": 1000}], result);
}
{
const pipeline = [{$match: {condition: 1}}, {$count: "quantity"}];
const result = coll.aggregate(pipeline).toArray();
assert.eq([{"quantity": 500}], result);
}