Index
Last updated
Was this helpful?
Last updated
Was this helpful?
An Index
represents a MongoDB database index, of any kind.
from marrow.mongo import Index
marrow.schema:
Attribute
Indexes are defined via the following attributes predominantly used as . The field references to include in the index are passed positionally, all other attributes may be passed as keyword arguments. Not all are utilized for each type of index, see the usage section below (or relevant MongoDB documentation for the index type) for details.
fields
A set of field references and their associated prefixes as strings. Initially defined as the positional arguments to the class constructor.
Required
unique
Should the index be constructed with a unique constraint?
DefaultFalse
Official Documentation
background
Create the index as a background operation. Note that if this is falsy, the foreground index build will block all other operations on the database.
DefaultTrue
sparse
Omit from the index documents that omit the field.
DefaultFalse
expire
Number of seconds after which to expire (cull/delete/remove) the record, declaring a "time-to-live" (TTL) index.
partial
A query filter (raw, or as constructed by field comparison or use of parametric helper) to use for partial indexing.
bucket
Bucket size for use with, and only appropriate for, geoHaystack indexes.
min
The lower inclusive boundary for the longitude and latitude values.
Default-180.0
max
The upper inclusive boundary for the longitude and latitude values.
Default180.0
Fields are referenced by their "attribute path", that is, a period-separated string representing the path to that field from the top-level Document class. For example, if you define a model with a name = Field('foo')
field, the path for this is "name"
. If you have an embedded document, say, an Address
with a city
field attached as addr = Embed(Address)
, referencing the City would be: "addr.city"
Beyond just the reference, indexes also use prefix symbols to identify the type of index, ordering, etc.
No prefix or +
— Ascending
-
— Descending
@
— Geo2D
%
— GeoHaystack
*
— GeoSphere
#
— Hashed
$
— Full Text
Define your document subclass and assign instances of Index
as attributes. The name of the attribute will be used as the MongoDB index name. To avoid potential collisions with fields or other attributes such as methods, it is recommended to prefix these attribute names with a single leading underscore.
You can then create the index by calling the create
method of the Index
object, passing in a collection:
If you are using any of the mix-in traits descendant from Collection
(such as Queryable
) then the create_collection
method will, by default, also discover and create any associated indexes.
adapt(*args, **kw)
— create a new copy of this index with adjustments applied. Takes the same arguments as the constructor, with any new fields declared being added to the existing set. Predominantly useful for extending (and overriding) indexes declared by mix-in traits.
create(collection, **kw)
— instruct MongoDB to construct and persist the index. If the index is not configured for background
construction, this will block other operations on the database until complete. Additional arguments are passed through to the eventual PyMongo collection.create_index
call. Also available via the PyMongo standard method name, create_index
.
drop(collection)
— instruct MongoDB to deconstruct and remove the index from the collection metadata. Also available via the PyMongo standard method name, drop_index
.
Official Documentation
Official Documentation
Official Documentation
Official Documentation
Official Documentation