【发布时间】:2017-03-03 12:55:18
【问题描述】:
我有一个用node编写的方法如下从集合中查询
query: function (model, conditon, options) {
console.log(conditon, options);
return new Promise(function (resolve, reject) {
options = options || {};
model.find(conditon, {}, options).exec(function (error, data) {
if (error)
reject(error);
resolve(data);
})
})
}
我想获取以下查询的数据,
db.getCollection('_event').find({}).sort({'metadata.popularity': 1}).limit(10)
我应该如何修改上面的方法来支持这个查询?
这就是我从另一个文件中调用查询函数的方式,
dbService.query(eventModel, { 'eventId': idRetrieved }, {});
}).then(data => dbService.disconnectDb(data))
.then(data => callback(data))
.catch((error) => {
dbService.disconnectDb(error).then(error => {
console.log(error);
callback({}, error);
})
【问题讨论】:
-
exec如果您不提供回调,则返回一个 Promise,那么您为什么要创建自己的 Promise?无论如何,sort和limit参数从何而来? -
@JohnnyHK 限制和排序我在 mongo db 上执行,我想知道如何更改上述代码以支持它?