我使用mongodb来获取名为froggers的集合,但我不知道名为query.who的变量的含义可以为我解释函数的含义
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);
});
});
});
};
答案 0 :(得分:0)
查询变量是包含字段用户的对象。
该字段获取username变量的值。
它的构造是为了适应collection.query函数。
现在此查询将获取user = username.
中的所有集合
所以如果username='albert'
查询将是strat:
collection.find({user:'alebrt'})
...........