如何使用mongoose自动降低数据(数组计数)的士气

如何使用mongoose自动降低数据(数组计数)的士气,mongoose,Mongoose,我有一个如下所示的模式: var thing = new Schema({ myId: { type: String, required: true }, objects: [objectSchema], objectCount: Number }); 我有一个API调用,它返回一个列表。该列表不需要对象的填充列表,只需要计数 var sourceList = function (req, res) { return SourceModel.find({}, '-objects

我有一个如下所示的模式:

var thing = new Schema({
  myId: { type: String, required: true },
  objects: [objectSchema],
  objectCount: Number
});
我有一个API调用,它返回一个列表。该列表不需要对象的填充列表,只需要计数

var sourceList = function (req, res) {
  return SourceModel.find({}, '-objects', function (err, people) {
    if (!err) {
      return res.send(people);
    } else {
      return console.log(err);
    }
  });
};

var sourceGet = function (req, res) {
  return SourceModel.findOne({myId: req.params.id}, function (err, person) {
    if (!err) {
      return res.send(person);
    } else {
      return console.log(err);
    }
  });
};

exports.initialize = function (app) {
  app.get('/api/thing', thingList);
  app.get('/api/thing/:id', thingGet);
};
在向数据库添加内容时,我正在读取用户上传的数据文件,并使用此文件向数据库中升级

Thing.findOneAndUpdate({myId: thing.id}, thing, {upsert: true}, callback);
我想让Mongoose为我跟踪计数,因此,从逻辑上讲,我想到了一个pre-hook,但它们不是没有访问文档的权限,或者至少是正在更新的字段/值

我可以执行
查找
来检查文档是否存在,然后执行
保存
更新
这将允许我使用
保存
预钩子,因为它可以访问文档,但仍会保留
更新
,而该钩子不存在

如何让mongoose为我自动跟踪数组计数,或者如果未存储,如何让mongoose返回带有计算计数的完整文档列表而不返回完整数组

我不想手动跟踪数组大小,这是ODM应该为我做的一部分