collection - Collection level operations

来源:互联网 发布:锤子bigbang软件 编辑:程序博客网 时间:2024/05/24 05:03

pymongo.ASCENDING = 1

pymongo.DESCENDING = -1

with_options()
Get a clone of this collection changing the specified settings.
bulk_write()
Send a batch of write operations to the server.
insert_one(document)
Insert a single document.
insert_many(documents, ordered=True)
Insert a list of documents.
replace_one(filter, replacement, upsert=False)
Replace a single document matching the filter.
The upsert option can be used to insert a new document if a matching document does not exist.
update_one(filter, update, upsert=False)
Update a single document matching the filter.
    filter: A query that matches the document to update.
    update: The modifications to apply.
    upsert (optional): If True, perform an insert if no documents match the filter.
update_many(filter, update, upsert=False)
Update one or more documents that match the filter.
delete_one(filter)
Delete a single document matching the filter.
delete_many(filter)
Delete one or more documents matching the filter.
aggregate(pipeline, **kwargs)
Perform an aggregation using the aggregation framework on this collection.
    pipeline: a list of aggregation pipeline stages
    **kwargs (optional): See list of options.
All optional aggregate parameters should be passed as keyword arguments to this method. Valid options include, but are not limited to:
: - allowDiskUse (bool): Enables writing to temporary files. When set to True, aggregation stages can write data to the _tmp subdirectory of the –dbpath directory. The default is False.
: - maxTimeMS (int): The maximum amount of time to allow the operation to run in milliseconds.
: - batchSize (int): The maximum number of documents to return per batch. Ignored if the connected mongod or mongos does not support returning aggregate results using a cursor, or useCursor is False.
: - useCursor (bool): Requests that the server provide results using a cursor, if possible. Ignored if the connected mongod or mongos does not support returning aggregate results using a cursor. The default is True. Set this to False when upgrading a 2.4 or older sharded cluster to 2.6 or newer (see the warning below).
find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, manipulate=True)
Query the database.
find_one(filter_or_id=None, *args, **kwargs)
Get a single document from the database.
find_one_and_delete(filter, projection=None, sort=None, **kwargs)
Finds a single document and deletes it, returning the document.
    filter: A query that matches the document to delete.
    projection (optional): a list of field names that should be returned in the result document or a mapping specifying the fields to include or exclude. If projection is a list “_id” will always be returned. Use a mapping to exclude fields from the result (e.g. projection={‘_id’: False}).
    sort (optional): a list of (key, direction) pairs specifying the sort order for the query. If multiple documents match the query, they are sorted and the first is deleted.
    **kwargs (optional): additional command arguments can be passed as keyword arguments (for example maxTimeMS can be used with recent server versions).
find_one_and_replace(filter, replacement, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)
Finds a single document and replaces it, returning either the original or the replaced document.
find_one_and_update(filter, update, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)
Finds a single document and updates it, returning either the original or the updated document.
count(filter=None, **kwargs)
Get the number of documents in this collection.
distinct(key, filter=None, **kwargs)
Get a list of distinct values for key among all documents in this collection.
create_index(keys, **kwargs)
Creates an index on this collection.
Takes either a single key or a list of (key, direction) pairs. The key(s) must be an instance of basestring (str in python 3), and the direction(s) must be one of (ASCENDING, DESCENDING, GEO2D, GEOHAYSTACK, GEOSPHERE, HASHED, TEXT).
Valid options include, but are not limited to:
: - name: custom name to use for this index - if none is given, a name will be generated.
: - unique: if True creates a uniqueness constraint on the index.
: - background: if True this index should be created in the background.
: - sparse: if True, omit from the index any documents that lack the indexed field.
: - bucketSize: for use with geoHaystack indexes. Number of documents to group together within a certain proximity to a given longitude and latitude.
: - min: minimum value for keys in a GEO2D index.
: - max: maximum value for keys in a GEO2D index.
: - expireAfterSeconds: Used to create an expiring (TTL) collection. MongoDB will automatically delete documents from this collection after seconds. The indexed field must be a UTC datetime or the data will not expire.
create_indexes(indexes)
Create one or more indexes on this collection.
drop_index(index_or_name)
Drops the specified index on this collection.
drop_indexes()
Drops all indexes on this collection.
list_indexes()
Get a cursor over the index documents for this collection.
index_information()
Get information on this collection’s indexes.
rename(new_name, **kwargs)
Rename this collection.
options()
Get the options set on this collection.
group(key, condition, initial, reduce, finalize=None, **kwargs)
Perform a query similar to an SQL group by operation.
insert(doc_or_docs, manipulate=True, check_keys=True, continue_on_error=False, **kwargs)
Insert a document(s) into this collection.
ensure_index(key_or_list, cache_for=300, **kwargs)
DEPRECATED - Ensures that an index exists on this collection
0 0