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?