Each Query
object must have a database operation associated with it. The database operation determines what action to take when connecting to the database—from finding documents to storing them. There are two ways to assign a database operation to a Query
object. One is to call the operation from the Model
object and not specify a callback. The Query
object returned has that operation assigned to it. For example:
var query = model.find();
Once you already have a Query
object, you can change the operation that is applied by calling the method on the Query
object. For example, the following code creates a Query
object that first applies a count()
operation and then a find()
operation. The where()
clause is applied to both:
var query = model.count();
query.where('value').lt(5);
query.exec(function(){});
query.find();
query.exec(function(){});
This allows you to dynamically reuse the same Query
object to perform multiple database operations. Table 16.2 lists the methods you can call on the Query
object. You can also use these methods on a compiled Model
object that can return a Query
object by omitting the callback function. Keep in mind that if you pass in a callback function to any of these methods, the operation will be executed and the callback called when finished.