-
Notifications
You must be signed in to change notification settings - Fork 119
Query Interface
Rows and Documents can be retrieved through several static helper methods of models.
Any model method which involves specifying query conditions can be executed two ways:
When a callback function:
is passed, the operation will be executed immediately with the results passed to the callback. is not passed, an instance of Query is returned, which provides a special QueryBuilder interface for you. Let's take a look at what happens when passing a callback:
- where
- gt
- gte
- lt
- lte
- ne
- [in, inq] (#in)
- nin
- regex
- like
- nlike
- sort, order
- group
- asc
- desc
- limit
- skip
- slice
- between
var Query = User.find();
Query.where('active', 1);
Query.order('id DESC');
Query.run({}, function(err, users) {
// your code here
});
var Query = User.find();
Query.where('userId', user.id);
Query.run({}, function(err, count){
// your code here
});
// the same as prev
User.find({where: {userId: user.id}}, function(err, users){
// your code here
});
Specifies a greater than expression.
Query.gt('userId', 100);
Query.where('userId').gt(100);
// the same as prev
User.find({
where: {
userId: {
gt : 100
}
}
}}, function(err, users){
// your code here
});
Specifies a greater than or equal to expression.
Query.gte('userId', 100);
Query.where('userId').gte(100);
// the same as prev
User.find({
where: {
userId: {
gte : 100
}
}
}}, function(err, users){
// your code here
});
Specifies a less than expression.
Query.lt('visits', 100);
Query.where('visits').lt(100);
// the same as prev
Post.find({
where: {
visits: {
lt : 100
}
}
}}, function(err, posts){
// your code here
});
Specifies a less than or equal to expression.
Query.lte('visits', 100);
Query.where('visits').lte(100);
// the same as prev
Post.find({
where: {
visits: {
lte : 100
}
}
}}, function(err, posts){
// your code here
});
Matches all values that are not equal to the value specified in the query.
Query.ne('userId', 100);
Query.where('userId').ne(100);
// the same as prev
User.find({
where: {
userId: {
ne : 100
}
}
}}, function(err, users){
// your code here
});
Matches any of the values that exist in an array specified in the query.
Query.in('userId', [1,5,7,9]);
Query.where('userId').in([1,5,7,9]);
// the same as prev
User.find({
where: {
userId: {
in : [1,5,7,9]
}
}
}}, function(err, users){
// your code here
});
Selects rows where values match a specified regular expression.
Query.regex('title', 'intel');
Query.where('title').regex('intel');
// the same as prev
Post.find({
where: {
title: {
regex : 'intel'
}
}
}}, function(err, posts){
// your code here
});
Pattern matching using a simple regular expression comparison.
Query.like('title', 'intel');
// the same as prev
Post.find({
where: {
title: {
like : 'intel'
}
}
}}, function(err, posts){
// your code here
});
Pattern not matching using a simple regular expression comparison.
Query.nlike('title', 'intel');
// the same as prev
Post.find({
where: {
title: {
nlike : 'intel'
}
}
}}, function(err, posts){
// your code here
});
Matches values that do not exist in an array specified to the query.
Query.nin('id', [1,2,3]);
// the same as prev
Post.find({
where: {
title : {
nin : [1,2,3]
}
}
}}, function(err, posts){
// your code here
});
Sets the sort column and direction.
Query.sort('title DESC');
Query.sort('title', 'DESC');
// the same as prev
Post.find({
order: 'title DESC'
}}, function(err, posts){
// your code here
});
Sets the group by column.
Query.group('title');
// is the same as
Post.find({
group: 'title'
}}, function(err, posts){
// your code here
});
Sets the sort column and direction ASC.
Query.asc('title');
// is the same as
Query.sort('title ASC');
// the same as prev
Post.find({
order: 'title ASC'
}}, function(err, posts){
// your code here
});
Sets the sort column and direction DESC.
Query.desc('title');
// is the same as
Query.sort('title DESC');
// the same as prev
Post.find({
order: 'title DESC'
}}, function(err, posts){
// your code here
});
The skip method specifies at which row the database should begin returning results.
Query.skip(10);
// the same as prev
Post.find({
skip: 10
}}, function(err, posts){
// your code here
});
The limit method specifies the max number of rows to return.
Query.limit(10);
// the same as prev
Post.find({
limit: 10
}}, function(err, posts){
// your code here
});
Limits the number of elements projected from an array. Supports skip and limit slices.
Query.slice([20,10]);
// the same as prev
Post.find({
skip: 20,
limit: 10
}}, function(err, posts){
// your code here
});
Check whether a value is within a range of values.
Query.between('created', ['2013-01-01','2013-01-08']);
// the same as prev
Post.find({
where: {
created: {
between : ['2013-01-01','2013-01-08']
}
}
}}, function(err, posts){
// your code here
});