def __copy__(self):
return MotorCursor(self.delegate.__copy__(), self.collection)
python类collection()的实例源码
def next_object(self):
"""Get next GridOut object from cursor."""
grid_out = super(MotorGridOutCursor, self).next_object()
if grid_out:
return MotorGridOut(self.collection, delegate=grid_out)
else:
# Exhausted.
return None
def __init__(self, collection, ordered):
self.io_loop = collection.get_io_loop()
delegate = BulkOperationBuilder(collection.delegate, ordered)
super(MotorBulkOperationBuilder, self).__init__(delegate)
def wrap(self, collection):
# Replace pymongo.collection.Collection with MotorCollection
return self[collection.name]
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
return MotorCollection(self.database, self.name + '.' + name)
def parallel_scan(self, num_cursors, **kwargs):
"""Scan this entire collection in parallel.
Returns a list of up to ``num_cursors`` cursors that can be iterated
concurrently. As long as the collection is not modified during
scanning, each document appears once in one of the cursors' result
sets.
For example, to process each document in a collection using some
function ``process_document()``::
@gen.coroutine
def process_cursor(cursor):
while (yield cursor.fetch_next):
process_document(document)
# Get up to 4 cursors.
cursors = yield collection.parallel_scan(4)
yield [process_cursor(cursor) for cursor in cursors]
# All documents have now been processed.
If ``process_document()`` is a coroutine, do
``yield process_document(document)``.
With :class:`MotorReplicaSetClient`, pass `read_preference` of
:attr:`~pymongo.read_preference.ReadPreference.SECONDARY_PREFERRED`
to scan a secondary.
:Parameters:
- `num_cursors`: the number of cursors to return
.. note:: Requires server version **>= 2.5.5**.
"""
command_cursors = yield self.__parallel_scan(num_cursors, **kwargs)
motor_command_cursors = [
MotorCommandCursor(cursor, self)
for cursor in command_cursors]
raise gen.Return(motor_command_cursors)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection
return self.database[obj.name]
elif obj.__class__ is Cursor:
return MotorCursor(obj, self)
elif obj.__class__ is CommandCursor:
return MotorCommandCursor(obj, self)
else:
return obj
def get_io_loop(self):
return self.collection.get_io_loop()
def clone(self):
"""Get a clone of this cursor."""
return MotorCursor(self.delegate.clone(), self.collection)
def __copy__(self):
return MotorCursor(self.delegate.__copy__(), self.collection)
def next_object(self):
"""Get next GridOut object from cursor."""
grid_out = super(MotorGridOutCursor, self).next_object()
if grid_out:
return MotorGridOut(self.collection, delegate=grid_out)
else:
# Exhausted.
return None
def __init__(self, collection, ordered):
self.io_loop = collection.get_io_loop()
delegate = BulkOperationBuilder(collection.delegate, ordered)
super(MotorBulkOperationBuilder, self).__init__(delegate)
def wrap(self, collection):
# Replace pymongo.collection.Collection with MotorCollection
return self[collection.name]
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
return MotorCollection(self.database, self.name + '.' + name)
def parallel_scan(self, num_cursors, **kwargs):
"""Scan this entire collection in parallel.
Returns a list of up to ``num_cursors`` cursors that can be iterated
concurrently. As long as the collection is not modified during
scanning, each document appears once in one of the cursors' result
sets.
For example, to process each document in a collection using some
function ``process_document()``::
@gen.coroutine
def process_cursor(cursor):
while (yield cursor.fetch_next):
process_document(document)
# Get up to 4 cursors.
cursors = yield collection.parallel_scan(4)
yield [process_cursor(cursor) for cursor in cursors]
# All documents have now been processed.
If ``process_document()`` is a coroutine, do
``yield process_document(document)``.
With :class:`MotorReplicaSetClient`, pass `read_preference` of
:attr:`~pymongo.read_preference.ReadPreference.SECONDARY_PREFERRED`
to scan a secondary.
:Parameters:
- `num_cursors`: the number of cursors to return
.. note:: Requires server version **>= 2.5.5**.
"""
command_cursors = yield self.__parallel_scan(num_cursors, **kwargs)
motor_command_cursors = [
MotorCommandCursor(cursor, self)
for cursor in command_cursors]
raise gen.Return(motor_command_cursors)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection
return self.database[obj.name]
elif obj.__class__ is Cursor:
return MotorCursor(obj, self)
elif obj.__class__ is CommandCursor:
return MotorCommandCursor(obj, self)
else:
return obj
def get_io_loop(self):
return self.collection.get_io_loop()
def clone(self):
"""Get a clone of this cursor."""
return MotorCursor(self.delegate.clone(), self.collection)
def __copy__(self):
return MotorCursor(self.delegate.__copy__(), self.collection)
def next_object(self):
"""Get next GridOut object from cursor."""
grid_out = super(MotorGridOutCursor, self).next_object()
if grid_out:
return MotorGridOut(self.collection, delegate=grid_out)
else:
# Exhausted.
return None