The strategy for sharing and reusing parts of the circuit in the mongoose.js file

Suppose I have two circuits, for example. for person and company . Both of them must have an address , which consists of street name , number , zip and city .

What would be the strategy to avoid copying the address properties between two schema definitions? I read about sub docs , but they seem (1) tied to the same parent scheme, (2) always occur in an array.

+7
database mongoose schema
source share
2 answers

Almost too obvious, but here is what I finally came up with:

Define the reusable parts separately, however, contrary to my first thoughts: do not use Schema here:

 var addressSubschema = { street: String, number: String, zip: String, city: String } 

Just include this part in real schemes:

 var personSchema = new mongoose.Schema({ name: { type: String, required: true }, title: { type: String }, address: addressSubschema }); var companySchema = new mongoose.Schema({ name: { type: String, required: true }, addresses: [addressSubschema] }); 
+13
source share

My (naive?) Approach could be to create a new scheme for the address and use a Mongoose set of mechanics to refer to the given ObjectID address. However, this is essentially an emulation of the behavior of relational databases, and I'm not sure how the PC is used when using flat storage like Mongo.

0
source share

All Articles