简体   繁体   中英

what't the meaning of the query in my code

I use the mongodb to fetch the collection which named froggers,but i don't know the meaning of the variable named query.who can explain the meaning of function for me

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

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'})

...........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM