List plugin for mongoose that allows pagination, filtering, sorting.
$ npm install mongoose-list
model.js
var mongoose = require('mongoose')
, schema
mongoose.plugin(require('../lib/mongoose-list'),{searchFields: ['name']})
schema = new mongoose.Schema({
name: {
type: String,
require: true
}
})
module.exports = mongoose.model('Test',schema)
app.js
var Model = require('./model')
//with sorting
Model.list({start: 0, limit: 10, sort: 'name'},function(err,count,results){
if(err) throw err
console.log('found ' + count + 'records')
results.forEach(function(row){
console.log('name: ' + row.name)
})
})
//with searching
Model.list({start: 0, limit: 10, sort: 'name', find: 'foo'},function(err,count,results){
if(err) throw err
console.log('found ' + count + 'records')
results.forEach(function(row){
console.log('name: ' + row.name)
})
})
- Variable
searchFields
- Default all non hidden schema paths
This option will limit what fields are considered searchable.
By default it will search any field that does not start with _
- Variable
start
- Default
0
Where to start showing records from. Also known as offset.
- Variable
limit
- Default
10
Limit of records to return in result set.
- Variable
sort
- Default
''
Uses a Mongoose style sort string eg: `+name -author'
- Variable
find
- Default
''
- Variable
populate
- Default
null
Uses a Mongoose populate to populate objectIds
Filter results by value applying to the searchFields
Alternatively, the find
variable can also be a custom mongoose query object like the following example:
var query = {
find: {
$or: [
{ field1: /something/ },
{ field2: new RegExp('else', 'i') }
]
}
}
Model.list({start: 0, limit: 10, sort: 'name', find: query},function(err,count,results){
if(err) throw err
console.log('found ' + count + 'records')
results.forEach(function(row){
console.log('name: ' + row.name)
})
})
This allows you to perform custom and complex queries and still make use of the remaining features of this module such as pagination.
- Adds populate feature to list plugin.
- Fixed issue with plugin crashing with out of range start and limit values.
- Added custom
find
object support that can be a direct mongoose query object rather than having one built automatically.
- Fixed bug with searching on non string fields
- Initial Release