Skip to content

docs(NODE-5961): aggregate projection example 1 #4040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,45 @@ describe('examples(project-fields-from-query):', function () {
});
}
});

it('Aggregation Projection Example 1', {
metadata: { requires: { mongodb: '>= 4.4.0' } },
test: async function () {
// Start Aggregation Projection Example 1
const cursor = db
.collection('inventory')
.find()
.project({
_id: 0,
item: 1,
status: {
$switch: {
branches: [
{
case: { $eq: ['$status', 'A'] },
then: 'Available'
},
{
case: { $eq: ['$status', 'D'] },
then: 'Discontinued'
}
],
default: 'No status found'
}
},
area: {
$concat: [{ $toString: { $multiply: ['$size.h', '$size.w'] } }, ' ', '$size.uom']
},
reportNumber: { $literal: 1 }
});
// End Aggregation Projection Example 1
const docs = await cursor.toArray();
for (const doc of docs) {
expect(doc).to.have.all.keys(['item', 'status', 'area', 'reportNumber']);
expect(doc).to.not.have.property('_id');
expect(doc.area).to.be.a('string');
expect(doc.reportNumber).to.equal(1);
}
}
});
});