Skip to content

cgkineo/jsonmongoquery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • $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
  • $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.
  • $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)
  • $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
  • $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
  • $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
  • $[] { 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

About

Mongodb query and update operators for json object arrays.

Resources

Stars

Watchers

Forks

Packages

No packages published