Sharing Top Content from the Angular-sphere.

A Node.js Perspective on MongoDB 3.6: $lookup and $expr

  • MongoDB 3.2 introduced the aggregation framework pipeline stage, which let you pull documents from a separate collection into your aggregation framework pipeline.
  • MongoDB 3.6 introduces support for much more sophisticated lookups with the new operator.
  • For example, the below pipeline finds all stock holdings whose current value is greater than $1000 based on the property from the holding document and the property from the stock document.
  • The new operator and the new and syntax for go a long way towards replicating SQL joins in MongoDB.
  • MongoDB 3.6 has several more exciting new aggregation features in addition to the new syntax, so make sure you upgrade to take advantage of all of the new features.

MongoDB 3.2 introduced the aggregation framework pipeline stage, which let you pull documents from a separate collection into your aggregation framework pipeline. Before MongoDB 3.6, could only do left outer joins with equality matching. In other words, suppose you had a collection of users, a collection of stocks, and a collection that mapped users to the stocks they hold. The stage can give you an array of stocks a user holds. But in MongoDB 3.2 and 3.4, could not give you just the stocks that had gone up in price since the customer bought them.

MongoDB 3.6 introduces support for much more sophisticated lookups with the new operator. In particular, allows you to do a that only pulls stock holdings that have appreciated in value. In this article, I’ll show you how to use the operator with queries, as well as with .

I’ll use Node.js and the MongoDB Node.js driver directly. You should use MongoDB driver or Mongoose because those are the the versions that support MongoDB 3.6.

The operator allows you to query based on computed properties. This is especially powerful with , but is also useful for queries. For example, let’s say you wanted to find all stock holdings where the total cost of the stock was more than $1000. In other words, find all documents in the ‘StockHolding’ collection where . In older versions of MongoDB you could do this with…

A Node.js Perspective on MongoDB 3.6: $lookup and $expr