

Just as with denormalizing in the “One-to-Many” case, you’ll want to consider the ratio of reads to updates. By telling MongoDB to only return the _id field, I reduce the network overhead down to just the few bytes that it takes to store that field (plus just a little bit more for the wire protocol overhead). ) to prevent MongoDB from having to ship the entire ‘hosts’ document over the network. ObjectID('F17C'), // reference to a different Partĭenormalizing would mean that you don’t have to perform the application-level join when displaying all of the part names for the product, but you would have to perform that join if you needed any other information about a part. ObjectID('AAAA'), // reference to the #4 grommet above Parts : [ // array of references to Part documents For reference, here’s the version of the Product document without denormalization.

An example will help make this clear.įor the parts example, you could denormalize the name of the part into the ‘parts’ array. This can eliminate the need to perform the application-level join for certain cases, at the price of some additional complexity when performing updates. Intermediate: Denormalizing With “One-To-Many” Relationshipsīeyond just modeling the various flavors of relationships, you can also add denormalization into your schema. This is OK for our task-tracking system: you need to consider if this works with your particular use case.) (And to the relational gurus who are reading this – you’re right: using this schema design means that it is no longer possible to reassign a Task to a new Person with a single atomic update.

#Dbschema mongodb design update
Specifically, you’ll have to update both the reference from the Person to the Task document, and the reference from the Task to the Person. Putting in the extra ‘owner’ reference into the Task document means that its quick and easy to find the Task’s owner, but it also means that if you need to reassign the task to another person, you need to perform two updates instead of just one. This design has all of the advantages and disadvantages of the “One-to-Many” schema, but with some additions. Owner: ObjectID("AAF1") // Reference to Person document You can optimize this by putting an additional reference to the Person in the Task document. On the other hand, in some other contexts this application will display a list of Tasks (for example, all of the Tasks in a multi-person Project) and it will need to quickly find which Person is responsible for each Task. Tasks [ // array of references to Task documents With the array of references to Task documents, a single Person document might look like this: The application will need to track all of the Tasks owned by a Person, so we will need to reference Person -> Task. There’s a “people” collection holding Person documents, a “tasks” collection holding Task documents, and a One-to-N relationship from Person -> Task. If you want to get a little bit fancier, you can combine two techniques and include both styles of reference in your schema, having both references from the “one” side to the “many” side and references from the “many” side to the “one” side.įor an example, let’s go back to that task-tracking system. With these basic techniques under our belt, I can move on to covering more sophisticated schema designs, involving two-way referencing and denormalization. What is the cardinality of the relationship: is it one-to-few one-to-many or one-to-squillions?.Will the entities on the “N” side of the One-to-N ever need to stand alone?.I also covered the two factors to consider when picking one of these designs: Last time I covered the three basic schema designs: embedding, child-referencing, and parent-referencing. This is the second stop on our tour of modeling One-to-N relationships in MongoDB. The gettersĪre useful for formatting or combining fields, while setters are useful forĭe-composing a single value into multiple values for storage.By William Zola, Lead Technical Support Engineer at MongoDB You can get and set but that do not get persisted to MongoDB. "_id index cannot be sparse" console.log(ssage) Title: String, // String is shorthand for ) Ĭonst Animal = mongoose.model( 'Animal', animalSchema) Each schema maps to a MongoDBĬollection and defines the shape of the documents within that collection. If you are migrating from 5.x to 6.x please take a moment to read the migration guide.Įverything in Mongoose starts with a Schema. If you haven't yet done so, please take a minute to read the quickstart to get an idea of how Mongoose works.
