Skip to content

Commit

Permalink
add top with sort condition
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Aug 19, 2015
1 parent 1c6888a commit 4579e07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ way 2: 4 params
done();
});
根据查询条件获取指定前xx条数,支持排序

// sort by "field" ascending and "test" descending
var query = {};
var sort = {field: 'asc', test: -1 };
Top.top(30, query, sort , function(err, tops){
if(err){
console.dir(err);
}

// console.dir(tops.length);
assert.equal(tops.length == 30, true);
done();
});

更改pagesize

Top.pagesize = 25;
Expand Down
9 changes: 8 additions & 1 deletion lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ MongooseDao.prototype.latest = MongooseDao.prototype.top = MongooseDao.prototype
var n;
var cb;
var q = {};
var sort = {};

// (num, cb)
if (arguments.length == 2) {
Expand All @@ -123,13 +124,19 @@ MongooseDao.prototype.latest = MongooseDao.prototype.top = MongooseDao.prototype
n = arguments[0];
q = arguments[1];
cb = arguments[2];
}else if (arguments.length == 4) {
// (num, {},cb)
n = arguments[0];
q = arguments[1];
sort = arguments[2];
cb = arguments[3];
}else{
// (cb)
n = this.pagesize;
cb = arguments[0];
}

this.model.find(q).limit(n).exec(cb);
this.model.find(q).sort(sort).limit(n).exec(cb);
};

// TODO: impl page by lastId
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoosedao",
"version": "1.0.12",
"version": "1.0.13",
"description": "mongoose dao",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4579e07

Please sign in to comment.