Is it better to split a complex mongoose pattern?

Assuming an API /login case where a user object from a collection should be returned for the corresponding set of credentials, which approach would be more efficient:

1) One model with projection requests:

 var UserSchema = new Schema({ name : String, email : String, dob : Number, phone : Number, gender : String, location : Object, // GeoJSON format {type: 'Point', geometry: { lat: '23.00', lng: '102.23' }} followers : [UserSchema], following : [UserSchema], posts : [PostSchema], groups : [GroupSchema] // ... and so on }); 

2) Separation models:

 var UserMinimalSchema = new Schema({ name : String, email : String, phone : Number, location : Object, }); var UserDetailSchema = new Schema({ dob : Number, gender : String, followers : [UserSchema], following : [UserSchema], posts : [PostSchema], groups : [GroupSchema] // ... and so on }); 

Say:

  • For a registered user, only id , name , email , phone and location should be returned.

  • The first model will use a projection query to return properties in (1).

  • In the second case, only UserMinimalSchema will be used to query the entire document.

  • In fact, both queries return exactly the same amount of data as indicated in (1).

  • Suppose the average user object has a limit of ~ 16 MB and there are 1 million records.

If someone has performed such a test / documentation link, it would be very helpful to see how important it is for separation or not.

+5
source share
1 answer

I would not use split models:

  • You will need to perform a population query every time you want to view all user data.
  • You increase the amount of data storage (now you have to refer to the user in your user details schema.
  • When Mongo goes looking, it will find links to model instances and will always retrieve the data that you specified in the projection request. It will not load the entire object into memory unless you specify it in your request.
+1
source

Source: https://habr.com/ru/post/1270836/


All Articles