nodejs学习(1):moogoose查询操作返回Query对象

最近刚接触nodejs,被方法返回值的处理着实郁闷了一下,因为很多查询结果需要在回调函数中读取,和java的绝大多数api使用的习惯大为不同。所以,先从最简单的nodejs使用moogoose中间件,返回Query对象记录起吧:

以下为moogoose使用find方法,官方的api提供的代码块。

// named john and at least 18
MyModel.find({ name: 'john', age: { $gte: 18 }});

// executes immediately, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// name LIKE john and only selecting the "name" and "friends" fields, executing immediately
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })

// passing options
MyModel.find({ name: /john/i }, null, { skip: 10 })

// passing options and executing immediately
MyModel.find({ name: /john/i }, null, { skip: 10 }, function (err, docs) {});

// executing a query explicitly
var query = MyModel.find({ name: /john/i }, null, { skip: 10 })
query.exec(function (err, docs) {});

// using the promise returned from executing a query
var query = MyModel.find({ name: /john/i }, null, { skip: 10 });
var promise = query.exec();
promise.addBack(function (err, docs) {});


你可能感兴趣的:(nodejs)