Indexrepresents a MongoDB database index, of any kind.
from marrow.mongo import Index
Indexes are defined via the following attributes predominantly used as arguments to the eventual
createIndexcall. 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.
A set of field references and their associated prefixes as strings. Initially defined as the positional arguments to the class constructor.
Should the index be constructed with a unique constraint?
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.
Omit from the index documents that omit the field.
Number of seconds after which to expire (cull/delete/remove) the record, declaring a "time-to-live" (TTL) index.
A query filter (raw, or as constructed by field comparison or use of parametric helper) to use for partial indexing.
Bucket size for use with, and only appropriate for, geoHaystack indexes.
The lower inclusive boundary for the longitude and latitude values.
The upper inclusive boundary for the longitude and latitude values.
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
cityfield attached as
addr = Embed(Address), referencing the City would be:
Beyond just the reference, indexes also use prefix symbols to identify the type of index, ordering, etc.
- No prefix or
$— Full Text
Define your document subclass and assign instances of
Indexas 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.
from marrow.mongo import Document, Index
from marrow.mongo.field import String, Integer
name = String()
age = Integer()
_age = Index('age')
You can then create the index by calling the
createmethod of the
Indexobject, passing in a collection:
# Connect and retrieve a handle to the target collection.
client = pymongo.MongoClient('mongodb://localhost/')
db = client.test
collection = db.people
# Create an index in that collection.
If you are using any of the mix-in traits descendant from
Queryable) then the
create_collectionmethod will, by default, also discover and create any associated indexes.
from marrow.mongo.trait import Queryable
class WikiPage(Queryable, Document):
__collection__ = 'pages'
id = String('_id')
content = String()
_fti = Index('$content')
WikiPage.bind(db) # Permit ActiveRecord-like usage.
# Alternatively, this can be called to create any missing 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
backgroundconstruction, this will block other operations on the database until complete. Additional arguments are passed through to the eventual PyMongo
collection.create_indexcall. Also available via the PyMongo standard method name,
drop(collection)— instruct MongoDB to deconstruct and remove the index from the collection metadata. Also available via the PyMongo standard method name,