Database / CouchDB Interview Questions
What are the query operators available in the Mango selector syntax?
Mango selectors are JSON objects where each key is a document field or a Mango operator. Operators begin with $. They fall into four groups: comparison, logical, element, and array operators.
POST /mydb/_find
{
"selector": {
"$and": [
{ "type": { "$eq": "product" } },
{ "price": { "$gte": 10, "$lte": 100 } },
{ "tags": { "$elemMatch": { "$eq": "sale" } } },
{ "discontinued": { "$exists": false } },
{ "name": { "$regex": "^Widget" } }
]
}
}
| Category | Operators | Description |
|---|---|---|
| Comparison | $eq, $ne, $lt, $lte, $gt, $gte | Equality and range comparisons |
| Logical | $and, $or, $not, $nor | Boolean combinations of conditions |
| Element | $exists, $type | Check field presence or JSON type |
| Array | $in, $nin, $all, $elemMatch, $size | Match values in or against arrays |
| Evaluation | $regex, $mod | Regex match; modulo arithmetic |
Important constraints: $regex queries do not use B-tree json indexes — they require a full-text (Lucene) index or fall back to a full scan. Compound conditions using $and can use a json index if all fields in the index prefix are covered by equality conditions. For best performance, structure selectors so the most selective equality conditions come first and match the leading fields of a json index.
More Related questions...