Created
August 26, 2014 18:35
-
-
Save greenlikeorange/07fed58767cf9c79962a to your computer and use it in GitHub Desktop.
Filter with Mongoose Schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
// Create Simple Post Schema | |
var Post = new Schema({ | |
owner: { type: Schema.Types.ObjectId, ref: 'User' }, | |
createdAt: { type: Date, default: new Date(), required: true }, | |
message: { type: String } | |
}); | |
// Filter Feature with limit, before, since | |
Post.prototype.filter = function(query, limit, before, since){ | |
// Set default limit to 25 | |
if ( limit == null ) limit = 25; | |
var que = this.find(query); | |
if( before ) que = que.where("_id").lt(before); | |
if( before ) que = que.where("_id").gt(after); | |
// Return matched query | |
return que.limit(limit); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About
Use to limit result of queried data from MongoDB
Parameters