MongoDB - Aggregation Framework - series #01
Let me demonstrate a few simple database queries on MongoDB using their Aggregation Pipeline Framework:
lpromise(
mgArr(dbEnum.nlp, collEnum.terms,
matchRegex("pronounc", /zh/),
sortAsc("hw"),
projectIncludeNoId("hw", "pronounc"),
)
);
/*
partial output:
{ hw: 'akathisia', pronounc: '=(a ku THEE zhu)' },
{ hw: 'ambrosia', pronounc: '=(am BROO zhu)' },
{ hw: 'amnesia', pronounc: '=(am NEE zhu)' },
{ hw: 'anesthesia', pronounc: '=(an is thee zhu)' },
*/
Notes:
The above example query retrieves every word that has a "zh" sound in its pronunciation.
The above is a functional abstraction around the stages. This is the same query using the raw stage syntax (which is in a JavaScript object format):
lpromise( mgArr(dbEnum.nlpdb, collEnum.terms_defs, { $match: { pronounc: /zh/ } }, { $sort: { hw: 1 } }, { $project: { _id: 0, hw: 1, pronounc: 1 } }, ) );
The pipeline of a Mongo aggregation pipeline is an array of query stages.
A stage is a JavaScript object containing stage keywords that start with a "$". This example above contains three stages: $match, $sort, $project.
The utility func, "mgArr" is a wrapper func that makes a call to the Mongo database. The "mg" is short for Mongo, and the "Arr" indicates the datatype it returns. The only other type we can return is obj, thus "mgObj" is the other util func.
The mgArr takes as arguments the (1) db name and (2) collection name and (3) the spread out set of 1 or more stages that comprise the actual database query.
I "spread" it so we don't have to use the array square brackets "[]" every time
$match is like "filter" or "where"
$project is how you limit what fields to return (to project) back to the caller. 0 means to skip this field (_id is the PK and always returns by default) 1 means to return that field
the lpromise is a utility func that logs the promise returned from the db.
What's next?
If anything is unclear let me know.
I'll be posting more examples and the utility funcs themselves over the coming days.