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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongodb-filter-object-parser

v1.5.0

Published

module that converts a string to an object that can be used in mongodb as a filter

Downloads

74

Readme

String to filter object converter for MongoDB

With this module you can make a MongoDB filter starting from a string that contains all arguments and operator.

Currently, the mongodb-filter-object-parser support 3 types of filter parser: search, textSearch and advancedSearch;

The first one focuses on queries that only want to get the documents containing the term/argument that was sent in any text field contained in a text index; textSearch accepts no-complete words as an argument and query fields defined in an array; and the advanced one offers high control on the query and needs syntax like value-field as argument.

Search

Supported use-cases

  • Get all documents containing one word or more in all text fields defined in a text index
  • Can be used logical operators like AND, OR & NOT.
  • Use two or more words as one argument.

Out of scope

  • Get only fully matched documents in response

  • Use substring as an argument

  • Advanced logic control with the use of parentheses.

  • Make the query in a specific Field of the document.

Syntax

Only pass a String with all arguments and operators separated by a space like:

(NOT) Value AND|OR|NOT value

Note: You can use multiple words as a single search term with double quotes ("words to search") or single quotes ('words to search')

Example:

Not blue and tall

As result will obtain

{
  "$text": {
    "$search": " -blue \"tall\""
  }
}

Text Search

Supported use-cases

  • Use regex to get only fully matched documents in response

  • Get all documents that contain one, part of, or more than one word in all text fields defined in the function.

  • Can be used logical operators like AND, OR & NOT.

  • Use two or more words as one argument.

Out of scope

  • Advanced logic control with the use of parentheses.
  • Make the query in a specific Field of the document.
  • Use a predefined text index of the collection.

Syntax

Only pass a String with all arguments and operators separated by a space like:

(NOT) Value AND|OR|NOT value

Note: You can use multiple words as a single search term with double quotes ("words to search") or single quotes ('words to search')

Example:

blue and tall

As result will obtain

{ 
    "$and": [
        { 
            "$or": [
                { "object.color": /tall\b/i }, 
                { "object.height": /tall\b/i } 
            ] 
        }, 
        { 
            "$or": [
                { "object.color": /blue\b/i }, 
                { "object.height": /blue\b/i } 
            ]  
        } 
    ] 
}

Configuration options

properties

The textSearch function requires this argument to know the fields to query, it must be an array of strings with all the fields

fullMatchOnly

Tells the function if the search will return only full matched (no substrings) documents in the query, this is still case insensitive.

Advanced search

Supported use-cases

  • Get documents containing the value defined in the field defined.
  • Has more control at deep-level, as Binary tree expression (at this moment, tree can grow to the left).
  • Can be used logical operators like NOT, AND & OR.
  • Value is case insensitive.
  • Queries with numerical range (float or integer)

Out of scope

  • Queries with date range are not available support currently.

Syntax

The syntax of the string will be like:

(not) (Value[Field]) or|and|not Value[Field]

Note: At this time, the tree can only grow to the left, all new arguments should be added at the end

Example:

((Green[Color] or Blue[Color]) or Red[Color]) and Medium[Size]

Note: You can use multiple words as a single search term with double quotes ("deep blue") or single quotes ('deep blue')

Based on Binary Expression Tree the string will be passed to an object

Binary expression tree

As a result we'll obtain an object:

{
  "$and": [
    {
      "Size": "Medium"
    },
    {
      "$or": [
        {
          "Color": "Red"
        },
        {
          "$or": [
            {
              "Color": "Green"
            },
            {
              "Color": "Blue"
            }
          ]
        }
      ]
    }
  ]
}

NEW!: Ranged queries

In version 1.4.0 a way to transform a string with a range query for numerical properties was added, this can be used with double colon between the 2 values to be queried. For example:

milk[productName] AND 1000::10000[quantity]

with the query string defined up, the filter obtained is:

{
  "$and":[
    {
      "productName": /milk/
    },
    {
      "quantity":{
        "$gte":1000
      }
    },
    {
      "quantity":{
        "$lte":10000
      }
    }
  ]
}

Note: Ranged query are only available for numbers, dates will be added in further updates


Installation

npm install mongodb-filter-object-parser

Usage

import { searchFilter, textSearchFilter, advancedSearchFilter } from "mongodb-object-filter-parser";

const simpleString = 'Not blue and tall';
const simpleFilter = searchFilter(simpleString);
const textFilter = textSearchFilter(simpleString,["color", "height"])

const advancedString = '(Blue[Color] or Red[Color]) and Medium[Size]';
const advancedFilter = advancedSearchFilter(advancedString);

//use trough mongodb in a ODM as mongoose
const query = Collection.find(simpleFilter);
const query = Collection.find(textFilter);
const query = Collection.find(advancedFilter);

License

Copyright 2021

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.