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

mongo-search-parameters

v0.2.2

Published

Easy find() search parameters

Downloads

137

Readme

Mongo Search parameters

Utility function that maps a JSON object to mongoose operators, works with node 10.

This is useful for forwarding graphql request params to either the mongoose model or the mongodb collection. Similar to how Prisma and Strapi work.

You pass the collection and the search parameters and get the back the expected result using for example

    const result = await useSearchParams(Test, {
      where: {
        title_contains: "lore",
      }
    });
    res.send(result);

Install

npm i mongo-search-parameters

OR

yarn add mongo-search-parameters

Demo

  • Mongoose: https://glitch.com/~mongo-search-parameters-plain
  • Apollo Graphql + Mongoose: https://glitch.com/~mongo-search-parameters-with-apollo-server

Usage

import useSearchParams from 'mongo-search-parameters';
import { mongooseModels } from 'path-to-your-mongoose-models';

const { User } = mongoModels;

// where (object): Define the operators to apply in the query.
const params = { // all fields are optional
  where: {
    name_contains: 'Test',
    age_gt: 21      
  },
  sort: 'name:desc', // it does accept also an array like ['name:desc', 'age:asc']
  limit: 10
}
// the above query will find all users which names contain (case insensitive) 'Test' and age is greater than 21, sort them by name from Z to A, limit them to just 10 rows if more are returned

export default async () => {
    const categories = await useSearchParams(User, { ...params });
    return categories
}

All mongo and mongoose chain methods and options are still allowed. For example for:

  • mongoose you can do await useSearchParams(User, { ...params }, { age: 1 }).lean()
  • mongo you can do await useSearchParams(User, { ...params }, { projection: { age: 1 } }).toArray()

Supported operators

  • [x] where | ne
  • [x] where | gt
  • [x] where | gte
  • [x] where | lt
  • [x] where | lte
  • [x] where | in
  • [x] where | nin
  • [x] where | contains
  • [x] where | containss
  • [x] where | matches
  • [x] where | matchess
  • [x] where | containsIndex
  • [x] where | containssIndex
  • [x] where | matchesIndex
  • [x] where | matchessIndex
  • [x] limit
  • [x] start
  • [x] sort

contains

name_contains: 'Mark Twain': the field name in the db is a string and contains the substring 'Mark Twain'. Mark twain, mark twain, mark Twain, etc. would be returned. It is case insentitive

containsIndex

name_containsIndex: 'Mark Twain': like name_contains but uses indexes. Do not forget to set a text index on the field name and that the MongoDB version you use supports text indexes as well

containss

name_contains: 'Mark': the field name in the db is a string and contains the substring 'Mark Twain'. Mark Twain, would be returned while mark Twain will not. It is case sensitive

containssIndex

name_containssIndex: 'Mark Twain': like name_containss but uses indexes. Do not forget to set a text index on the field name and that the MongoDB version you use supports text indexes as well

matches

name_matches: 'Mark Sculby': the field name in the db is a string and matches the substring 'Mark' OR the substring 'Sculby'. Mark Johnson, Vincent Skulby, etc. would be returned. It is case insentitive

matchesIndex

name_matchesIndex: 'Mark Twain': like name_matches but uses indexes. Do not forget to set a text index on the field name and that the MongoDB version you use supports text indexes as well

matchess

name_matchess: 'Mark Sculby': the field name in the db is a string and matches the substring 'Mark' OR the substring 'Sculby'. Mark Johnson, Vincent Skulby, etc. would be returned while mark Johnson would not. It is case sentitive

matchessIndex

name_matchessIndex: 'Mark Twain': like name_matchess but uses indexes. Do not forget to set a text index on the field name and that the MongoDB version you use supports text indexes as well

ne

age_ne: 21: the field age does not equal to 21. Can be used with any field type.

gt

age_gt: 21: greater than

gte

age_gte: 21: greater than or equal to

lt

age_lt: 21: less than

lte

age_lte: 21: greater than or equal to

in

age_in: [21, 26, 27, 28, 21]: true if field is contained in the array of values supplied. Accepts also single value like age_in: 21

nin

age_nin: [21]: true if field is NOT contained in the array of values supplied

TODO

  • [x] Add mongodb client compatibility
  • [x] Add demo
  • [x] Refine search and explain different behaviors
  • [x] Allow to search using text indexes
  • [ ] Implement OR operator