🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

mongoose-query-parser

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-query-parser

Convert url query string to MongooseJs friendly query object including advanced filtering, sorting, population, string template, type casting and many more...

1.0.1
Source
npm
Version published
Weekly downloads
1.6K
-28.04%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-query-parser

Convert url query string to MongooseJs friendly query object including advanced filtering, sorting, population, string template, type casting and many more...

The library is built highly inspired by api-query-params

Features

  • Powerful. Supports most of MongoDB operators ($in, $regexp, …) and features (paging, projection, population, type casting, string templates…)
  • Custom. Allows customization of keys (ie, fields v.s. select) and options
  • Agnostic. Works with any web frameworks (Express …) and/or Mongoose/MongoDb libraries

Usage

API

import { MongooseQueryParser } from 'mongoose-query-parser';

const parser = new MongooseQueryParser(options?: ParserOptions)
parser.parse(query: string, predefined: any) : QueryOptions
Arguments
  • ParserOptions: object for advanced options (See below) [optional]
  • query: query string part of the requested API URL (ie, firstName=John&limit=10). Works with already parsed object too (ie, {status: 'success'}) [required]
  • predefined: object for predefined query context [optional]

Returns

  • QueryOptions: object contains the following properties:
    • filter which contains the query criteria
    • populate which contains the query population. Please see Mongoose Populate for more details
    • select which contains the query projection
    • sort, skip, limit which contains the cursor modifiers for paging purpose

Example

import { MongooseQueryParser } from 'mongoose-query-parser';

const parser = new MongooseQueryParser();
const predefined = {
  vip: { name: { $in: ['Google', 'Microsoft', 'NodeJs'] } }
  sentStatus: 'sent'
};
const query = parser.parse('${vip}&status=${sentStatus}&timestamp>2017-10-01&author.firstName=/john/i&limit=100&skip=50&sort=-timestamp&select=name&populate=children', predefined);
{
  filter: {
    { name: { $in: ['Google', 'Microsoft', 'NodeJs'] } },
    status: 'sent',
    timestamp: { $gt: Fri Jan 01 2017 01:00:00 GMT+0100 (CET) },
    'author.firstName': /john/i
  },
  sort: { timestamp: -1 },
  skip: 50,
  limit: 100,
  select: { name },
  populate: [{ path: 'children'}]
}

Supported features

Filtering operators

MongoDBURIExampleResult
$eqkey=valtype=public{filter: {type: 'public'}}
$gtkey>valcount>5{filter: {count: {$gt: 5}}}
$gtekey>=valrating>=9.5{filter: {rating: {$gte: 9.5}}}
$ltkey<valcreatedAt<2016-01-01{filter: {createdAt: {$lt: Fri Jan 01 2016 01:00:00 GMT+0100 (CET)}}}
$ltekey<=valscore<=-5{filter: {score: {$lte: -5}}}
$nekey!=valstatus!=success{filter: {status: {$ne: 'success'}}}
$inkey=val1,val2country=GB,US{filter: {country: {$in: ['GB', 'US']}}}
$ninkey!=val1,val2lang!=fr,en{filter: {lang: {$nin: ['fr', 'en']}}}
$existskeyphone{filter: {phone: {$exists: true}}}
$exists!key!email{filter: {email: {$exists: false}}}
$regexkey=/value/<opts>email=/@gmail\.com$/i{filter: {email: /@gmail.com$/i}}
$regexkey!=/value/<opts>phone!=/^06/{filter: {phone: { $not: /^06/}}}

For more advanced usage ($or, $type, $elemMatch, etc.), pass any MongoDB query filter object as JSON string in the filter query parameter, ie:

parser.parse('filter={"$or":[{"key1":"value1"},{"key2":"value2"}]}');
//  {
//    filter: {
//      $or: [
//        { key1: 'value1' },
//        { key2: 'value2' }
//      ]
//    },
//  }

Keywords

querystring

FAQs

Package last updated on 27 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts