Skip to content

Commit a5b0b2c

Browse files
committed
Adds AggregateFunctions.limit() and changes Readme to avoid confusion (dresende#172)
1 parent ea3e93d commit a5b0b2c

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎Readme.md‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,27 +437,32 @@ Person.aggregate({ surname: "Doe" }).min("age").max("age").get(function (err, mi
437437
console.log("The youngest Doe guy has %d years, while the oldest is %d", min, max);
438438
});
439439
```
440-
Here's an example to illustrate how to use groupby:
440+
441+
An `Array` of properties can be passed to select only a few properties. An `Object` is also accepted to define conditions.
442+
443+
Here's an example to illustrate how to use `.groupBy()`:
444+
441445
```js
442446
//The same as "select avg(weight), age from person where country='someCountry' group by age;"
443447
Person.aggregate(["age"], { country: "someCountry" }).avg("weight").groupBy("age").get(function (err, stats) {
444448
// stats is an Array, each item should have 'age' and 'avg_weight'
445449
});
446450
```
447451

448-
Possible aggregating functions:
452+
### Base `.aggregate()` methods
453+
454+
- `.limit()`: you can pass a number as a limit, or two numbers as offset and limit respectively
455+
- `.order()`: same as `Model.find().order()`
456+
457+
### Additional `.aggregate()` methods
449458

450459
- `min`
451460
- `max`
452461
- `avg`
453462
- `sum`
454463
- `count` (there's a shortcut to this - `Model.count`)
455464

456-
### Available options
457-
458-
- `offset`: discards the first `N` elements
459-
- `limit`: although it can be passed as a direct argument, you can use it here if you prefer
460-
- `only`: if you don't want all properties, you can give an array with the list of properties you want
465+
There are more aggregate functions depending on the driver (Math functions for example).
461466

462467
#### Chaining
463468

‎lib/AggregateFunctions.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ function AggregateFunctions(opts) {
3535
group_by = Array.prototype.slice.apply(arguments);
3636
return this;
3737
},
38+
limit: function (offset, limit) {
39+
if (typeof limit == "number") {
40+
opts.limit = [ offset, limit ];
41+
} else {
42+
opts.limit = [ 0, offset ]; // offset = limit
43+
}
44+
return this;
45+
},
3846
order: function (property, order) {
3947
opts.order = [ property, order ];
4048
return this;
@@ -87,6 +95,9 @@ function AggregateFunctions(opts) {
8795
if (opts.order) {
8896
query.order.apply(query, opts.order);
8997
}
98+
if (opts.limit) {
99+
query.offset(opts.limit[0]).limit(opts.limit[1]);
100+
}
90101

91102
opts.driver.execQuery(query.build(), function (err, data) {
92103
if (err) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
common.createConnection(function (err, db) {
5+
common.createModelTable('test_aggregate_limit', db.driver.db, function () {
6+
common.insertModelData('test_aggregate_limit', db.driver.db, [
7+
{ id : 1, name : 'test1' },
8+
{ id : 2, name : 'test1' },
9+
{ id : 3, name : 'test2' },
10+
{ id : 4, name : 'test2' },
11+
{ id : 5, name : 'test2' },
12+
{ id : 6, name : 'test3' },
13+
{ id : 7, name : 'test3' }
14+
], function (err) {
15+
if (err) throw err;
16+
17+
var TestModel = db.define('test_aggregate_limit', common.getModelProperties());
18+
19+
TestModel.aggregate().distinct('name').limit(1).get(function (err, names) {
20+
assert.equal(err, null);
21+
assert.equal(Array.isArray(names), true);
22+
assert.equal(names.length, 1);
23+
db.close();
24+
});
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)