Skip to content

Commit

Permalink
docs(NODE-5961): aggregate projection example 1 (#4040)
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Mar 15, 2024
1 parent aec8416 commit 159ea81
Showing 1 changed file with 41 additions and 0 deletions.
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);
}
}
});
});

0 comments on commit 159ea81

Please sign in to comment.