Mongoose和Angular Memory优化

时间:2016-01-30 22:18:35

标签: angularjs node.js mongodb optimization

我正在尝试找到适当的方法来针对针对mongo的查询和传递回UI的内容优化我的应用程序。

如果我们有一个如下所述的简单架构,每个配置文件可能拥有数百个对象权限和数千个字段权限:

     // 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]
    });

UI的第一个状态是简单地显示“个人档案”列表,并允许用户单击并显示所选个人档案的“对象权限”。从这里,用户可以单击任何对象配置文件并查看该对象的字段权限。

没什么难的,我的代码按要求工作。我的问题是优化查询和内存管理。

1)我已经搜索但无法找到你是否可以让mongo只返回没有兄弟文件的profile文件(objectPermissions和fieldPermissions),我相信答案是否定的,但是想知道是否有办法做到这一点。

2)如果1的答案为否,那么,当返回初始页面的数据时,明智的做法是将未使用的兄弟数组设置为空,这样我们就不会通过网络传递大量未使用的数据了吗? 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)或者,在将数据存储到mongo并确保两个文档保持同步时,我是否应该为此摘要页面创建一个更小的更优化的文档?

4)UI的初始数据有效载荷大约为500KB - 我是否担心优化?

1 个答案:

答案 0 :(得分:1)

  1. 在您的查询中使用select('name modifiedDat')以避免手动取消这些大字段。您也可以使用lean() lean ref
  2. 你是对的。
  3. 根据您的使用情况,我建议您进行分页(使用来自mongoose的skiplimit)并根据文档大小限制document max size ref
  4. 以这种方式设置架构
    // 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.我不知道。您可以使用 express morgan 来查看响应时间和检查。这是链接https://github.com/expressjs/morgan#dev或者您可以使用chrome开发人员工具来衡量时间。

相关问题