Skip to content

Instantly share code, notes, and snippets.

@greenlikeorange
Created August 26, 2014 18:35
Show Gist options
  • Save greenlikeorange/07fed58767cf9c79962a to your computer and use it in GitHub Desktop.
Save greenlikeorange/07fed58767cf9c79962a to your computer and use it in GitHub Desktop.
Filter with Mongoose Schema
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);
}
@greenlikeorange
Copy link
Author

About

Use to limit result of queried data from MongoDB

Parameters

  • query is query object use find on database
  • limit is return limit of found result
  • before is an object_id use to find items before that id is create
  • after is an object_id use to find items after that id is created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment