collection – A mongo collection

class mongotor.client.Client(database, collection)
insert(*args, **kwargs)

Insert a document

Parameters:
  • doc_or_docs: a document or list of documents to be inserted
  • safe (optional): check that the insert succeeded?
  • check_keys (optional): check if keys start with ‘$’ or contain ‘.’, raising InvalidName in either case
  • callback : method which will be called when save is finished
remove(*args, **kwargs)

remove a document

Parameters:
  • spec_or_id: a query or a document id
  • safe (optional): safe insert operation
  • callback : method which will be called when save is finished
update(*args, **kwargs)

Update a document(s) in this collection.

Parameters:
  • spec: a dict or SON instance specifying elements which must be present for a document to be updated
  • document: a dict or SON instance specifying the document to be used for the update or (in the case of an upsert) insert - see docs on MongoDB `update modifiers`_
  • upsert (optional): perform an upsert if True
  • safe (optional): check that the update succeeded?
  • multi (optional): update all documents that match spec, rather than just the first matching document. The default value for multi is currently False, but this might eventually change to True. It is recommended that you specify this argument explicitly for all update operations in order to prepare your code for that change.
find_one(*args, **kwargs)

Get a single document from the database.

All arguments to find() are also valid arguments for find_one(), although any limit argument will be ignored. Returns a single document, or None if no matching document is found.

Parameters:
  • spec_or_id (optional): a dictionary specifying the query to be performed OR any other type to be used as the value for a query for "_id".
  • **kwargs (optional): any additional keyword arguments are the same as the arguments to find().
find(*args, **kwargs)

Query the database.

The spec argument is a prototype document that all results must match. For example:

Parameters:
  • spec (optional): a SON object specifying elements which must be present for a document to be included in the result set
  • fields (optional): a list of field names that should be returned in the result set (“_id” will always be included), or a dict specifying the fields to return
  • skip (optional): the number of documents to omit (from the start of the result set) when returning the results
  • limit (optional): the maximum number of results to return
  • timeout (optional): if True, any returned cursor will be subject to the normal timeout behavior of the mongod process. Otherwise, the returned cursor will never timeout at the server. Care should be taken to ensure that cursors with timeout turned off are properly closed.
  • snapshot (optional): if True, snapshot mode will be used for this query. Snapshot mode assures no duplicates are returned, or objects missed, which were present at both the start and end of the query’s execution. For details, see the snapshot documentation.
  • tailable (optional): the result of this find call will be a tailable cursor - tailable cursors aren’t closed when the last data is retrieved but are kept open and the cursors location marks the final document’s position. if more data is received iteration of the cursor will continue from the last document received. For details, see the tailable cursor documentation.
  • sort (optional): a list of (key, direction) pairs specifying the sort order for this query. See sort() for details.
  • max_scan (optional): limit the number of documents examined when performing the query
  • read_preferences (optional): The read preference for this query.
count(callback)

Get the size of the results among all documents.

Returns the number of documents in the results set

distinct(key, callback)

Get a list of distinct values for key among all documents in this collection.

Raises TypeError if key is not an instance of basestring (str in python 3).

To get the distinct values for a key in the result set of a query use distinct().

Parameters:
  • key: name of key for which we want to get the distinct values
aggregate(*args, **kwargs)

Perform an aggregation using the aggregation framework on this collection.

Parameters:
  • pipeline: a single command or list of aggregation commands
  • read_preference

Note

Requires server version >= 2.1.0

group(*args, **kwargs)

Perform a query similar to an SQL group by operation.

Returns an array of grouped items.

The key parameter can be:

  • None to use the entire document as a key.
  • A list of keys (each a basestring (str in python 3)) to group by.
  • A basestring (str in python 3), or Code instance containing a JavaScript function to be applied to each document, returning the key to group by.
Parameters:
  • key: fields to group by (see above description)
  • condition: specification of rows to be considered (as a find() query specification)
  • initial: initial value of the aggregation counter object
  • reduce: aggregation function as a JavaScript string
  • finalize: function to be called on each object in output list.

Previous topic

database – Database level operations

Next topic

orm – Map a mongo collection into a python class

This Page