0

I am trying to find appropriate methods to optimise my application with respect to queries against mongo and what is being passed back to the UI.

If we have a simple schema as described below, where each Profile may have hundreds of object permissions and thousands of fields permissions:

     // schema element in objectPermission.js
    module.exports = mongoose.Schema({
      object: String,
      name: String,
      readable: Boolean,
      editable: Boolean
    });

     // schema element in fieldPermission.js
    module.exports = mongoose.Schema({
      allowCreate: Boolean,
      allowDelete: Boolean,
      allowEdit: Boolean,
      allowRead: Boolean,
      modifyAllRecords: Boolean,
      object: String,
      viewAllRecords: Boolean
    });

     // model definition in Profile.js
    module.exports = mongoose.model('Profile', {
      name: String,
      modifiedDate: Date,
      objectPermissions: [objectPermission],
      fieldPermissions: [fieldPermission]
    });

The first state of the UI is to simply show a list of Profiles and allow the user to click into and be shown the Object Permissions for the chosen profile. From here, the user could click into any Object Profile and see the field permissions for that object.

Nothing to difficult and my code is working as required. My question is around optimisation of queries and memory management.

1) I have searched for but unable to find if you can have mongo only return the Profile document with no sibling documents (objectPermissions and fieldPermissions), I believe the answer is no but wondered if there is a way to do this.

2) If the answer to 1 is no, then, when returning the data for initial page is it wise to set the unused sibling arrays to null so that we are not passing a huge amount of unused data over the wire? e.g.

function getProfiles(req, res, removeSiblings) {
  dataService.findProfiles()
    .then(function(records) {
      if (removeSiblings) {
        for (var i = 0; i < records.length; i++) {
          records[i].objectPermissions = null;
          records[i].fieldPermissions = null;
        }
      }
      res.json(_buildSuccessObject('', records));
    })
    .catch(function(err) {
      res.json(_buildErrorObject('Error getting profile records ' + err, null));
    })
    .done(function() {
      console.log('Done Loading Profiles');
    });
};

3) Or, should I be creating a smaller more optimised document for this summary page when storing the data into mongo and ensuring the two documents are kept in sync?

4) The initial payload of data to the UI is around 500KB - am I worrying to much about optimisation?

1 Answer 1

1
  1. use select('name modifiedDat') in your query to avoid manually nullify those big fields. you can also use lean() lean ref
  2. you are correct.
  3. from your usage, I would advise you do pagination(with skip and limit from mongoose) and set schemas in this way due to document size limit document max size ref

// schema element in objectPermission.js
module.exports = mongoose.Schema({
  profileId: {type:Schema.Types.ObjectId, ref:'Profile'}, //index profileId field can also boost up the performance
  object: String,
  name: String,
  readable: Boolean,
  editable: Boolean
});

 // schema element in fieldPermission.js
module.exports = mongoose.Schema({
  profileId: {type:Schema.Types.ObjectId, ref:'Profile'}, //index profileId field can also boost up the performance
  allowCreate: Boolean,
  allowDelete: Boolean,
  allowEdit: Boolean,
  allowRead: Boolean,
  modifyAllRecords: Boolean,
  object: String,
  viewAllRecords: Boolean
});

 // model definition in Profile.js
module.exports = mongoose.model('Profile', {
  name: String,
  modifiedDate: Date,
});

4.I don't know. you could probably use express morgan to see the response time and check.This is the link https://github.com/expressjs/morgan#dev Or you can use chrome developer tools to measure the time.

Sign up to request clarification or add additional context in comments.

1 Comment

I am a fool! I was going down the filter route and lost myself in the wrong part of the document, thank you - this worked perfectly! Also thank you for the other pointers, I will read through and see what else I can learn!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.