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

jsonmongoquery

v1.0.5

Published

Mongodb query and update operators for json object arrays.

Downloads

20

Readme

jsonmongoquery

Mongodb query and update operators for json object arrays.

Usage

import { queryToPredicate, updateToPredicate, context } from 'jsonmongoquery'

// Fix current date in a context
const context = new Context({ date: new Date() })

// Load some data
const data = [
  { _id: 1, item: { name: 'ab', code: '123' }, qty: 15, tags: ['A', 'B', 'C'] },
  { _id: 2, item: { name: 'cd', code: '123' }, qty: 20, tags: ['B'] },
  { _id: 3, item: { name: 'ij', code: '456' }, qty: 25, tags: ['A', 'B'] },
  { _id: 4, item: { name: 'xy', code: '456' }, qty: 30, tags: ['B', 'A'] },
  { _id: 5, item: { name: 'mn', code: '000' }, qty: 20, tags: [['A', 'B'], 'C'] }
]

// Query will select items where qty = 20
const filterQuery = { qty: { $eq: 20 } }
// Make an filter predicate function from the filter query, validate the query according to the query schema
const filterPredicate = queryToPredicate(filterQuery, { validate: true, context })
// Filter the data according to the predicate
const filtered = data.filter(filterPredicate)
console.log(filtered)

// Query will remove the last item from the tags array
const updateQuery = { $pop: { tags: 1 } }
// Make an update predicate function from the update query, validate the query according to the update schema
const updatePredicate = updateToPredicate(updateQuery, { validate: true, context })
// Filter the data according to the predicate
const updated = filtered.filter(updatePredicate)
console.log(updated)

Supported operators

Comparison query operators

  • $eq { field: { $eq: value } }, { field: value } Matches documents where the value of a field equals the specified value
  • $gt { field: { $gt: value } } Selects those documents where the value of the specified field is greater than (i.e. >) the specified value
  • $gte { field: { $gte: value } } Selects the documents where the value of the specified field is greater than or equal to (i.e. >=) a specified value
  • $in { field: { $in: [value1, ...] } } Selects the documents where the value of a field equals any value in the specified array
  • $lt { field: { $lt: value } } Selects the documents where the value of the field is less than (i.e. <) the specified value
  • $lte { field: { $lte: value } } Selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value
  • $ne { field: { $ne: value } } Selects the documents where the value of the specified field is not equal to the specified value. This includes documents that do not contain the specified field
  • $nin { field: { $nin: [value1, ...] } } Selects the documents where the specified field value is not in the specified array or the specified field does not exist

Logical query operators

  • $and { $and: [{ expression1 }, ...] } Performs a logical AND operation on an array of one or more expressions. Selects the documents that satisfy all the expressions.
  • $not { field: { $not: { operator-expression } } } Performs a logical NOT operation on the specified operator-expression and selects the documents that do not match the operator-expression. This includes documents that do not contain the field.
  • $nor { $nor: [{ expression1 }, ...] } Performs a logical NOR operation on an array of one or more query expression and selects the documents that fail all the query expressions in the array.
  • $or { $or: [{ expression1 }, ...] } Performs a logical OR operation on an array of one or more expressions and selects the documents that satisfy at least one of the expressions.

Element query operators

  • $exists { field: { $exists: boolean } } Matches documents that contain or do not contain a specified field, including documents where the field value is null
  • $type { field: { $type: JSONType } } Selects documents where the value of the field is an instance of the specified JSON type(s)

Evaluation query operators

  • $mod { field: { $mod: [divisor, remainder] } } Select documents where the value of a field divided by a divisor has the specified remainder
  • $regex { field: { "$regex": "pattern", "$options": "options" } } Provides regular expression capabilities for pattern matching strings in queries
  • $where { $where: "return true" } Pass a string containing a JavaScript expression

Array query operators

  • $all { field: { $all: [ value1, ... valueN ] } } Selects the documents where the value of a field is an array that contains all the specified elements
  • $elemMatch { field: { $elemMatch: { query1, ... queryN } } } Matches documents that contain an array field with at least one element that matches all the specified query criteria
  • $size { field: { $size: number } } Matches any array with the number of elements specified by the argument

Field update operators

  • $currentDate { $currentDate: { field1: true, field2: { $type: "date" }, field3: { $type: "timestamp" } }, ... } Sets the value of a field to the current date, either as a Date or a timestamp
  • $inc { $inc: { field1: amount1, ... } } Increments a field by a specified value
  • $min { $min: { field1: value1, ... } } Updates the value of the field to a specified value if the specified value is less than the current value of the field
  • $max { $max: { field1: value1, ... } } Updates the value of the field to a specified value if the specified value is greater than the current value of the field
  • $mul { $mul: { field1: number1, ... } } Multiply the value of a field by a number
  • $rename {$rename: { field1: newName1, ... } } Updates the name of a field
  • $set { $set: { field1: value1, ... } } Replaces the value of a field with the specified value
  • $unset { $unset: { field1: "", ... } } Deletes a particular field

Array update operators

  • $[] { update-operator: { "array.$[]": value } } Indicates that the update operator should modify all elements in the specified array field
  • $addToSet { $addToSet: { field1: value1, ... } } Adds a value to an array unless the value is already present
  • $pop { $pop: { field: -1 | 1, ... } } Removes the first or last element of an array
  • $pull { $pull: { field1: value|condition, ... } } Removes from an existing array all instances of a value or values that match a specified condition
  • $push { $push: { field1: value1, ... } } Appends a specified value to an array
  • $each { $push: { field: { $each: [ value1, ... ] } }, $addToSet: { field: { $each: [ value1, ... ] } } } Modifier is available for use with the $addToSet operator and the $push operator.
  • $position { $push: { field: { $each: [ value1, ... ], $position: num } } } Modifier specifies the location in the array at which the $push operator inserts elements
  • $slice { $push: { field: { $each: [ value1, ... ], $slice: num } } } Modifier limits the number of array elements during a $push operation
  • $sort { $push: { field: { $each: [ value1, ... ], $sort: -1 | 1 | { field1: 1, field2: -1, ... } } } Modifier orders the elements of an array during a $push operation
  • $pushAll { $pullAll: { field1: [ value1, ... ], ... } } Removes all instances of the specified values from an existing array

Supported JSONTypes

A subset of BSON Types https://www.mongodb.com/docs/manual/reference/bson-types/#bson-types

| name | value | order | | ---- | ----- | ----- | | undefined | 0 | 0 | | null | 10 | 1 | | number | 1 | 2 | | string | 2 | 3 | | object | 3 | 4 | | array | 4 | 5 | | bool | 8 | 8 |

Notes

  1. Lexicographic vs numeric order is not currently supported https://www.mongodb.com/docs/manual/reference/operator/update/#behavior
  2. 100% unit test coverage, from the mongodb website examples, for each supported operator, for each supported type
  3. Query intellisense support using typescript types
  4. Query schemas and validation supplied
  5. $where can be used as an update operator, $where: "this.title = this.title?.replace(/1 to 5/g, '1 to 10'); return true;"

Unsupported operators

Evaluation query operators

Geospatial query operators

Bitwise query operators

Projection operators

Miscellaneous query operators

Field update operators

Array update operators

Bitwise update operators