npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-find-and-filter

v1.0.0

Published

Mongoose find method on steroids with filtering and sorting features

Downloads

14

Readme

Mongoose find and filter plugin

NPM

mongoose-find-and-filter plugin is a mongoose plugin which improves the find method with filtering and sorting features. You can also limit or skip results and populate refs.

Installation

npm install mongoose-find-and-filter --save

You can use this plugin with specific schema or globally for all schemas. To use it with specific schema:

const mongooseFindAndFilter = require('mongoose-find-and-filter');

const userSchema = new Schema({...});

// attach mongoose-find-and-filter plugin to userSchema
userSchema.plugin(mongooseFindAndFilter);

If you want to use the plugin globally for all schemas:

const mongoose = require('mongoose');
const mongooseFindAndFilter = require('mongoose-find-and-filter');

mongoose.plugin(mongooseFindAndFilter);

After attaching mongoose-find-and-filter plugin you will have Model.findAndFilter(...) method on your model.

Usage

Mongoose find and filter plugin provides support for both promise style as well as callback style queries. Setup for your backend API server (eg. ExpressJS) might look something like this:

Promise example

const User = require('./user.model.js'); // model

/* this is example of User model instance
{
  "_id": "58e0dc934ee10d2f1c2cd0f4",
  "username": "johndoe",
  "contact": "58e68c594ee10d1870493af1", // ref to Contact model
  "updatedAt": "2017-04-05T05:31:00.522Z",
  "createdAt": "2017-02-16T08:32:17.959Z",
  "job": "58f0ab4e62697f2db41b2e69", // ref to Job model
  "age": 10,
  "email": "[email protected]",
  "surname": "doe",
  "firstName": "john",
  "password": "sdlkfjldksfjcdslkjdslkfjsdflkdsjkfsdfjsdfkf"
}
*/

let query = { age: { $gte: 1 } }; // default query
let queryParams = req.query; // query params from FE
let protectedKeys = ['email']; // protected keys == not searchable/filterable
let populate = [{
    path: 'contact',
    model: 'Contact',
    populate: {
      path: 'city',
      model: 'City'
    }
  },
  { path: 'job', model: 'Job' }
]; // populate refs

User
  .findAndFilter(query, queryParams, protectedKeys, populate)
  .then((users) => {
    // users will be documents with age>1 and further filtered by query sent as query params from FE
    res.status(200).json(users);
  })
  .catch((err) => {
    return next(err);
  });

Callback example

const User = require('./user.model.js'); // model

let query = { age: { $gte: 0 } }; // default query
let queryParams = req.query; // query params from FE
let protectedKeys = ['email']; // protected keys == not searchable/filterable
let populate = [{
    path: 'contact',
    model: 'Contact',
    populate: {
      path: 'city',
      model: 'City'
    }
  },
  { path: 'job', model: 'Job' }
];

User
  .findAndFilter(query, queryParams, protectedKeys, populate,
    (err, users) => {
      if (err) return next(err);
      // users will be documents with age>1 and further filtered by query sent as query params from FE
      res.status(200).json(users);
    });

Then if you want to query your endpoint send request in one of the following forms:

curl http://localhost:5005/api/v1/users // all users will be returned
curl http://localhost:5005/api/v1/users?username=johndoe // only docs with username=johndoe will be returned
curl http://localhost:5005/api/v1/users?username=johndoe&select=age username // only docs with username=johndoe will be returned and only age and username keys will be selected
curl http://localhost:5005/api/v1/users?username=johndoe&limit=5 // max 5 docs will be returned
curl http://localhost:5005/api/v1/users?username=johndoe&sort=_id // only docs with username=johndoe sorted by _id in ASC order will be returned
curl http://localhost:5005/api/v1/[email protected] // request will respond with 400 bad request as email is protected key
curl http://localhost:5005/api/v1/users?_id=58e0dc934ee10d2f1c2cd0f4 // only doc with _id=58e0dc934ee10d2f1c2cd0f4 will be returned

License

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004

Copyright (C) 2017 Jozef Butko [email protected]

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  1. You just DO WHAT THE FUCK YOU WANT TO.