Mongoose没有从mongo db返回计数

时间:2013-11-15 04:45:32

标签: node.js mongodb mongoose

之前我们使用的是mongoose版 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获得所需的记录,如果已有记录,则会将计数返回为

现在我们已将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

0 个答案:

没有答案