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)
})
})
searchFields
This option will limit what fields are considered searchable.
By default it will search any field that does not start with _
start
0
Where to start showing records from. Also known as offset.
limit
10
Limit of records to return in result set.
sort
''
Uses a Mongoose style sort string eg: `+name -author'
find
''
populate
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.
find
object support that can be a direct mongoose query object rather than having
one built automatically.Language | javascript |
Version | 0.2.2 |
Git URL | https://github.com/nullivex/mongoose-list |
License | MIT |
Description | |
Keywords |