Add More Relations
In Getting Started we created two models โ country and city โ with a single one-to-many relation. Now let's grow that relation: store the 50 most populous cities of each country, pick a capital, and add a brand-new user model with a many-to-many relation.
Managing Relations
So far we've created three models โ country, city, and user โ with different relationships between them. Can we update these relationships after the documents already exist?
The addRelation Function
In addition to the usual CRUD functions, every model exposes two relation-management functions: addRelation and removeRelation. Use them to change the relations of an existing document. This tutorial covers addRelation โ both the many-to-many and the one-to-many cases.
The removeRelation Function
removeRelation is the inverse of addRelation โ it undoes links between documents, cleaning up both sides. This tutorial covers removing a many-to-many link, then removing an optional single relation.
Find and findOne
When you want to penetrate one step into the depth of relationships, find and findOne are the best choices. In a traditional Mongo setup you'd need an aggregation with a $lookup for a left join โ but since Lesan embeds all relations, a plain find/findOne already returns one level of relations: the document and its directly-related children.
Aggregation
Use aggregation when you need to penetrate more than one step in the depth of relationships โ going from father to grandson, or vice versa. Don't worry: every $lookup, $unwind, and $project you need is generated for you. You only write the filtering logic.
findOneAndUpdate
Updating is the most challenging part of Lesan. Updating one document may require updating thousands โ or even millions โ of other documents. The good news: Lesan does it automatically. This tutorial walks through the two scenarios (updating a user, updating a country), then explains the three solutions Lesan proposes for the worst case.
deleteOne
Deletion has the same problems as update โ removing a document may require touching many others. This tutorial covers deleting a simple document (a user) and deleting one with dependents (a country), plus the hardCascade option.
insertMany
Another Lesan surprise: insertMany keeps all relationships embedded correctly while inserting many documents at once. It's fast โ changes go to the database as a single aggregation pipeline.
Procurement Workflow
8 items
ZiWound Case Study
4 items