Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I better off splitting an elaborate mongoose model?

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

1) One model with projection queries:

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) Split 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
});

Let's say:

  1. For a logged-in user, only id, name, email, phone and location are to be returned.

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

  3. In the second case, only the UserMinimalSchema would be used to query the entire document.

  4. Essentially both queries return exactly the same amount of data as mentioned in (1).

  5. Assume that average user object is ~16MB limit and there are 1 Million records.

If someone performed such a test/links to documentation, it would be of great help to see how much it will matter to split or not.

like image 747
Amresh Venugopal Avatar asked Aug 13 '17 20:08

Amresh Venugopal


1 Answers

I would not use split models:

  1. You'll have to perform a population query everytime you want to look all of the user's data
  2. You're increasing your data storage (you now will have to reference the user in your user details schema.
  3. When Mongo will go do a lookup, it will find references to model instances and only extract data that you've specified in your projection query anyways. It will not load the entire object into memory unless you specify that in your query.
like image 166
Govind Rai Avatar answered Oct 31 '22 04:10

Govind Rai