MongoDB Query Operators
As mentioned previously, MongoDB has quite a rich set of query operators and predicates. In Table 2-2 we provide a table with the meaning of each one, along with a sample usage and the SQL equivalent where applicable.
Table 2-2. MongoDB query operators
Operator | Meaning | Example | SQL Equivalent |
---|---|---|---|
$gt | Greater Than | “score”:{"$gt”:0} | > |
$lt | Less Than | “score”:{"$lt”:0} | < |
$gte | Greater Than or Equal | “score”:{"$gte”:0} | >= |
$lte | Less Than or Equal | “score”:{"$lte”:0} | ⇐ |
$all | Array Must Contain All | “skills”:{"$all”:["mongodb”,"python"]} | N/A |
$exists | Property Must Exist | “email”:{"$exists”:True} | N/A |
$mod | Modulo X Equals Y | “seconds”:{"$mod”:[60,0]} | MOD() |
$ne | Not Equals | “seconds”:{"$ne”:60} | != |
$in | In | “skills”:{"$in”:["c”,"c++"]} | IN |
$nin | Not In | “skills”:{"$nin”:["php”,"ruby”,"perl"]} | NOT IN |
$nor | Nor | “$nor”:[{"language”:"english"},{"country”:"usa"}] | N/A |
$or | Or | “$or”:[{"language”:"english"},{"country”:"usa"}] | OR |
$size | Array Must Be Of Size | “skills”:{"$size”:3} | N/A |
If you do not fully understand the meaning or purpose of some of these operators immediately do not worry. We shall discuss the practical use of some of the more advanced operators in detail in Chapter 3.
Get MongoDB and Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.