Skip to content

Commit 159ea81

Browse files
docs(NODE-5961): aggregate projection example 1 (#4040)
1 parent aec8416 commit 159ea81

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/integration/node-specific/examples/project_fields_from_query_results.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,45 @@ describe('examples(project-fields-from-query):', function () {
219219
});
220220
}
221221
});
222+
223+
it('Aggregation Projection Example 1', {
224+
metadata: { requires: { mongodb: '>= 4.4.0' } },
225+
test: async function () {
226+
// Start Aggregation Projection Example 1
227+
const cursor = db
228+
.collection('inventory')
229+
.find()
230+
.project({
231+
_id: 0,
232+
item: 1,
233+
status: {
234+
$switch: {
235+
branches: [
236+
{
237+
case: { $eq: ['$status', 'A'] },
238+
then: 'Available'
239+
},
240+
{
241+
case: { $eq: ['$status', 'D'] },
242+
then: 'Discontinued'
243+
}
244+
],
245+
default: 'No status found'
246+
}
247+
},
248+
area: {
249+
$concat: [{ $toString: { $multiply: ['$size.h', '$size.w'] } }, ' ', '$size.uom']
250+
},
251+
reportNumber: { $literal: 1 }
252+
});
253+
// End Aggregation Projection Example 1
254+
const docs = await cursor.toArray();
255+
for (const doc of docs) {
256+
expect(doc).to.have.all.keys(['item', 'status', 'area', 'reportNumber']);
257+
expect(doc).to.not.have.property('_id');
258+
expect(doc.area).to.be.a('string');
259+
expect(doc.reportNumber).to.equal(1);
260+
}
261+
}
262+
});
222263
});

0 commit comments

Comments
 (0)