Lucene Filters
The filter parameter of the document list, the search body, count, aggregate, export, copy, and update/delete many is a Lucene query. The server converts it into a MongoDB filter.
author:Tolkien AND year:[1950 TO 1960]
title:"The Hobbit"
-status:archived
price:>=10
_exists_:isbn
name:Joh*Long filters can go in the request body: POST …/documents/search, POST …/documents/count, POST …/documents/export and filter in the aggregate body. Since 3.0 many/update and many/delete take { "$lucene": "<query>" } as their body filter (filter of the update body, the whole body of the delete); a filter in the query and in the body answers 400. The typed client (useMongocampCollection().find(), findPage, paginated) sends filters longer than 2000 characters as POST …/search by itself; there a read scope sees the operation search instead of list.
A filter that cannot be parsed answers 400 (code 4001) with the reason in x-error-additional-info. Fuzzy (city:plain~), regular expression (city:/pl.*/) and boost (city:plain^2) queries are not supported and also answer 400.
Terms and values
- A term is compared with the converted value: number, boolean, date or text.
- A quoted value is exact:
*and?inside quotes are literal (name:"100*"finds the text100*), and a value that converts matches the converted value or the same text.note:"2024-06-15T12:00:00Z"finds both a stored date and a text that looks like a date;zip:"12345"finds the number and the text. - A term is a date only as a full date with a four-digit year (
2014-04-20,2014-04-20T10:00, with or without a zone) or20140420T100000Z. Partial forms such as007,2014-04andT10:00are text. - Ranges:
[a TO b](inclusive),{a TO b}(exclusive), open ends with*, or>=,>,<=,<. field:*and_exists_:fieldmatch documents that have the field.- On a declared reference a term with an id (24 hex characters) matches the
ObjectIdand the text (2.6):customerId:6650f1c2a1b2c3d4e5f60718._idalways works that way. - An empty filter (
filter=) matches all documents. Update and delete many still needmatchAll=truefor it.
Boolean rules
Groups follow Lucene:
- every required clause (
+a,a AND b) matches, - no prohibited clause (
-a,NOT a) matches, - optional clauses (
a b,a OR b) count only when there is no required clause, and then one of them has to match.
| Filter | Matches |
|---|---|
a AND b OR c | a and b (c is optional) |
a OR b AND c | b and c |
a -b | a and not b |
a b -c | (a or b) and not c |
-(a AND b) | not both |
-a -b | every document except a and b |
Write the grouping you mean with parentheses: (a AND b) OR c.
Wildcards
* and ? match case-sensitively, so a prefix (name:abc*) can use an index on the field. Send filterCaseInsensitive=true to ignore the case. That reads the whole index or collection.
usePaginatedFind('users', { filter: 'name:abc*', filterCaseInsensitive: true })A prefix or wildcard made of number characters also matches numbers by their decimal text: n:1* finds 10 and 123, and price:*.5 finds 2.5. Such a filter cannot use an index.
On _id, a prefix or wildcard of hex characters also matches ObjectIds by their hex text, ignoring the case: _id:6ad7e7*, _id:*f60718. Since 3.0 a prefix (_id:6ad7e7*) is the range of ObjectIds that start with it, which the _id index serves; a wildcard inside the text (_id:*f60718) cannot use an index.
A quoted value that looks like a date, a number or a boolean matches the converted value and the same text (2.1): title:"2024-01-01" finds the text 2024-01-01 in a text field and the date in a date field.
A wildcard is never quoted. Escape a space instead: name:John\ Do*.
Dates without a zone
A date without Z or an offset (at:2014-04-20, also in ranges) is read in UTC, the same on every host. Set FILTER_TIME_ZONE to read them in another zone (Europe/Berlin, +05:30). In a zone with daylight saving, a time in the gap of the change is not a date and stays text. In the overlap, the earlier instant counts.
A bare date (2024-06-15) is midnight of that day. As the inclusive upper bound of a range it covers the whole day: at:[* TO 2024-06-15] and at:<=2024-06-15 include 2024-06-15T23:59. An exclusive upper bound ({* TO 2024-06-15}) or one with a time stays as written. For one day: at:[2024-06-15 TO 2024-06-15].
