0

我使用mongodb来获取名为froggers的集合,但我不知道名为query的变量的含义。谁能为我解释一下函数的含义

exports.get = function get(username, callback) {
  mongodb.open(function(err, db) {
    if (err) {
      return callback(err);
    }
    // 获取 froggers 集合
    db.collection('froggers', function(err, collection) {
      if (err) {
        mongodb.close();
        return callback(err);

      // 查找 user 属性为 username 的文档,如果 username 是 null 则匹配全部
      var query = {};
      if (username) {
        query.user = username;
      }
      collection.find(query).sort({time: -1}).toArray(function(err, docs) {
        mongodb.close();
        if (err) {
          callback(err, null);
        }
        // 封裝 froggers 为 Frogger 对象
        var froggers = [];   //定义frogger数组对象

        docs.forEach(function(doc, index) {
          var frogger = new Frogger(doc.user, doc.post, doc.time);
          froggers.push(frogger);
        });
        callback(null, posts);
      });
    });
  });
};
4

1 回答 1

0

the query variable is an object containing the field user. that field get the value of the username variable. it was constructed in order to fit the collection.query function. now this query will fetch all the collection in which user = username. so if username='albert' the query will strat :

collection.find({user:'alebrt'})

...........

于 2013-05-22T09:26:58.873 回答