Skip to content

Instantly share code, notes, and snippets.

View jasjar's full-sized avatar

Jason Jarrett jasjar

View GitHub Profile
@jasjar
jasjar / query-options.js
Last active January 19, 2023 20:13
Mongoose query - use limit, sort, fields, paging options
const getStories = (category, fields, sort, limit, page, callback) => {
//default values for these should be moved to config module
const f = (fields && fields !== 'all') ? fields.replace(new RegExp(',', 'g'), ' ') : (fields !== 'all') ? 'id category headline teaser author create_date' : null;
const s = (sort) ? sort.replace(new RegExp(',', 'g'), ' ') : '-create_date';
const l = (limit) ? Number(limit) : 10;
const p = (page) ? Number(page) : 0;
//Story here is a previously create Mongoose model
let query = Story.find({ category: category });