Mongoose Orchestrator
Mongoose is the probably the most popular MongoDB driver for NodeJS.
The "populate" problem.
You can't confuse a .populate
with what would be a JOIN
in relational database. Seems obvious but is a common mistake for someone coming from a relational database background.
You might be thinking "Alright, it's doing more than a single query, so what? At least you don't have data redundancy."
No, disk space is cheap but time isn't. Disk space is one of the cheapest things in computers, while doing multiple queries get a page response could make
The method itself is super fine and there are a chillion cases where is the best choice. But frequently, you would like to just save data denormalized.
The plan.
Provide a way to simplify this common operation.
How it works?
That animation is a representation of what happens under-the-hood with entities defined over the following schemas:
const mongoose = require('mongoose');
const mongooseOrchestrator = require('mongoose-orchestrator');
const ObjectId = mongoose.Schema.Types.ObjectId;
// Create and register the `Season` model.
const SeasonSchema = new mongoose.Schema({
name: {type: String, required: true}
});
mongoose.model('Season', SeasonSchema);
// Create and register the `Episode` model.
const EpisodeSchema = new mongoose.Schema({
name: {type: String, required: true},
season: {type: ObjectId, ref: 'Season'},
seasonName: {type: String, ref: 'Season.name', sync: true}
});
EpisodeSchema.plugin(mongooseOrchestrator);
mongoose.model('Episode', EpisodeSchema);
—
Now go check it out!
Subscribe to RECodes
Get the latest posts delivered right to your inbox