Querying with smoqe

The smoqe.to_mongo() function converts a smoqe query string or list into a MongoDB dict.

If you are using pymongo to interface with MongoDB, then you can use the drop-in replacement smoqe.MongoClient.

Extending smoqe

The whole point of smoqe is to be a simple query language. Only part of the full MongoDB syntax is supported. However, keep in mind that smoqe.to_mongo() outputs a dictionary, which can be extended further, so you can use smoqe to simplify just the simple parts of queries. For example, to join two simple experessions with $nor

import smoqe
q = {'$nor':[smoqe.to_mongo('a > 10'), smoqe.to_mongo('b < 12')]}

All the syntax details of the query language are given in smoqe.to_mongo().

Performance

Since smoqe simply translates queries to MongoDB syntax, unless you have a ridiculously large query (thousands of expressions) the performance of the query will be practically identical to its MongoDB equivalent.

API Documentation

smoqe.to_mongo(qry)

Transform a simple query with one or more filter expressions into a MongoDB query expression.

Parameters:qry (str or list) – Filter expression(s), see function docstring for details.
Returns:MongoDB query
Return type:dict
Raises:BadExpression, if one of the input expressions cannot be parsed

Expressions have three parts, called in order field, operator, and value.

  • field is the name of a field in a MongoDB document

  • value is the value to compare against:
    • numeric
    • string, you MUST use ‘single’ or “double” quotes
    • boolean: true, false
  • operator is a comparison operator:
    • inequalities: >, <, =, <=, >=, !=
    • PCRE regular expression: ~
    • data type: int, float, string, or bool
    • exists: boolean (true/false) whether field exists in record
    • size: for array fields, an inequality for the array size, given as a suffix to the operator: size>, size<

Multiple expressions can be a single string, or a list. In either case, the form is a “disjunction of conjunctions”.

In the string form:

  • “and” joins expressions into groups
  • “or” joins one or more expression groups

In the list form:

  • The inner list is a group of “and”ed expressions
  • The outer list “or”s the expression groups together.

In the string form, parentheses before or after the “or”ed expression groups [note: even non-sensical ones like ‘(((‘], are ignored. So these can be used to clarify the groupings.

Examples

Two sets of filters, return records where either is true:

>>> to_mongo('(a > 3 and b = "hello") or (c > 1 and d = "goodbye")')
{'$or': [{'a': {'$gt': 3}, 'b': 'hello'}, {'c': {'$gt': 1}, 'd': 'goodbye'}]}

Same as previous, but without parentheses:

>>> to_mongo('a > 3 and b = "hello" or c > 1 and d = "goodbye"')
{'$or': [{'a': {'$gt': 3}, 'b': 'hello'}, {'c': {'$gt': 1}, 'd': 'goodbye'}]}

Same as previous, but using lists rather than “and”/”or”:

>>> to_mongo([['a > 3', 'b = "hello"'], ['c > 1', 'd = "goodbye"']])
{'$or': [{'a': {'$gt': 3}, 'b': 'hello'}, {'c': {'$gt': 1}, 'd': 'goodbye'}]}
class smoqe.MongoClient(host=None, port=None, max_pool_size=10, document_class=<type 'dict'>, tz_aware=False, _connect=True, **kwargs)

Drop-in replacement for pymongo.MongoClient.

Three functions are enabled:

  • find
  • update
  • remove

In each case, the spec argument will be transparently pre-processed as a smoqe query if it is a string or list.

Table Of Contents

Previous topic

smoqe: Simplified MongoDB Query Expressions

This Page