1
我有节点写入的方法如下从收集查询如何与猫鼬一起执行排序和限制查询?
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'会返回一个承诺,那么为什么您要创建自己的承诺?无论如何,排序和限制参数来自哪里? – JohnnyHK
@JohnnyHK限制和排序我在mongo db上执行,我想知道如何改变上面的代码来支持它? – Sajeetharan