0

以前我们使用的是猫鼬版本mongoose@3.0.1。为了获得特定过滤条件的匹配计数,我们使用了 model.js 中的以下计数函数:

Model.count = function (conditions, callback) {
  var query = new Query(conditions).bind(this, 'count');
  if ('undefined' == typeof callback)
    return query;
  var cQuery;
  if (cQuery = this._cumulativeQuery) {
    merge(query._conditions, cQuery._conditions);
    if (query.options && cQuery.options)
      merge(query.options, cQuery.options);
    delete this._cumulativeQuery;
  }
  if (!query.model) query.bind(this, 'count');
  return query.count(callback);
};

在上述函数的帮助下,我们将从 mongodb 获取所需的记录,如果已经存在记录,它将返回计数为 1

现在我们已经将 mongoose 版本更新为mongoose@3.6.18。为了获得特定过滤条件的匹配计数,我们使用了 model.js 中的以下计数函数:

Model.count = function count (conditions, callback) {
  if ('function' === typeof conditions)
    callback = conditions, conditions = {};

  var query = new Query(conditions).bind(this, 'count');
  if ('undefined' == typeof callback)
    return query;

  this._applyNamedScope(query);
  return query.count(callback);
};

Model._applyNamedScope = function _applyNamedScope (query) {
  var cQuery = this._cumulativeQuery;

  if (cQuery) {
    merge(query._conditions, cQuery._conditions);
    if (query._fields && cQuery._fields)
      merge(query._fields, cQuery._fields);
    if (query.options && cQuery.options)
      merge(query.options, cQuery.options);
    delete this._cumulativeQuery;
  }

  return query;
}

在上述函数的帮助下,我们需要从 mongodb 获取所需的记录,如果已经存在记录,它将返回计数为 null 而不是 1,如上述情况,它甚至不更新现有记录。请就如何解决此问题提出建议。

下面我提到了我们连接到 mongodb 并尝试获取记录计数的方式。

    var mongodbconn = mongoose.createConnection('mongodb://user:pass@localhost:port/database');
    var usersschema =new Schema({
                    _id: String,
                    MemID:String,
                    Name:String
                }); 
    var users_model= mongodbconn.model('users', usersschema);
         users_model.count({MemID: '1717268'}, function (err, c) {
  if(err){
    console.log(err.stack+" Records Cnt: "+c);
  }

                 console.log("Count---------------->        " + c);
           });

得到以下响应:

 Count---------------->        null

MongoError: [object Object]
    at toError (/usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:110:11)
    at /usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:157:34
    at /usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1670:9
    at Server.Base._callHandler (/usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:382:41)
    at /usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:472:18
    at MongoReply.parseBody (/usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (/usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:430:20)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (/usr/local/lib/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:191:13)
    at EventEmitter.emit (events.js:98:17) Records Cnt: null
4

0 回答 0