本文整理汇总了Python中pymongo.collection.Collection方法的典型用法代码示例。如果您正苦于以下问题:Python collection.Collection方法的具体用法?Python collection.Collection怎么用?Python collection.Collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.collection
的用法示例。
在下文中一共展示了collection.Collection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mongo_date_range_query
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_mongo_date_range_query():
self = create_autospec(TickStore)
self._collection = create_autospec(Collection)
self._symbol_query.return_value = {"sy": {"$in" : ["s1" , "s2"]}}
self._collection.aggregate.return_value = iter([{"_id": "s1", "start": dt(2014, 1, 1, 0, 0, tzinfo=mktz())},
{"_id": "s2", "start": dt(2014, 1, 1, 12, 0, tzinfo=mktz())}])
self._collection.find_one.side_effect = [
{'e': dt(2014, 1, 1, 15, 0, tzinfo=mktz())},
{'e': dt(2014, 1, 2, 12, 0, tzinfo=mktz())}]
query = TickStore._mongo_date_range_query(self, 'sym', DateRange(dt(2014, 1, 2, 0, 0, tzinfo=mktz()),
dt(2014, 1, 3, 0, 0, tzinfo=mktz())))
assert self._collection.aggregate.call_args_list == [call([
{"$match": {"s": {"$lte": dt(2014, 1, 2, 0, 0, tzinfo=mktz())}, "sy": {"$in" : ["s1" , "s2"]}}},
{"$project": {"_id": 0, "s": 1, "sy": 1}},
{"$group": {"_id": "$sy", "start": {"$max": "$s"}}},
{"$sort": {"start": 1}}])]
assert self._collection.find_one.call_args_list == [
call({'sy': 's1', 's': dt(2014, 1, 1, 0, 0, tzinfo=mktz())}, {'e': 1}),
call({'sy': 's2', 's': dt(2014, 1, 1, 12, 0, tzinfo=mktz())}, {'e': 1})]
assert query == {'s': {'$gte': dt(2014, 1, 1, 12, 0, tzinfo=mktz()), '$lte': dt(2014, 1, 3, 0, 0, tzinfo=mktz())}}
示例2: test_concat_and_rewrite_checks_chunk_count
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_concat_and_rewrite_checks_chunk_count():
self = create_autospec(NdarrayStore)
collection = create_autospec(Collection)
version = {}
previous_version = {'_id': sentinel.id,
'base_version_id': sentinel.base_version_id,
'version': sentinel.version,
'segment_count' : 3,
'append_count' : 1,
'up_to': sentinel.up_to}
symbol = sentinel.symbol
item = sentinel.item
collection.find.return_value = [{'compressed': True, 'segment': 1},
{'compressed': False, 'segment': 2}]
with pytest.raises(DataIntegrityException) as e:
NdarrayStore._concat_and_rewrite(self, collection, version, symbol, item, previous_version)
assert str(e.value) == 'Symbol: sentinel.symbol:sentinel.version expected 1 segments but found 0'
示例3: test_concat_and_rewrite_checks_written
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_concat_and_rewrite_checks_written():
self = create_autospec(NdarrayStore)
collection = create_autospec(Collection)
version = {'_id': sentinel.version_id,
'segment_count': 1}
previous_version = {'_id': sentinel.id,
'up_to': sentinel.up_to,
'base_version_id': sentinel.base_version_id,
'version': sentinel.version,
'segment_count' : 5,
'append_count' : 3}
symbol = sentinel.symbol
item = []
collection.find.return_value = [{'_id': sentinel.id, 'segment': 47, 'compressed': True, 'sha': 'abc0'},
{'_id': sentinel.id_2, 'segment': 48, 'compressed': True, 'sha': 'abc1'},
# 3 appended items
{'_id': sentinel.id_3, 'segment': 49, 'compressed': False, 'sha': 'abc2'},
{'_id': sentinel.id_4, 'segment': 50, 'compressed': False, 'sha': 'abc3'},
{'_id': sentinel.id_5, 'segment': 51, 'compressed': False, 'sha': 'abc4'}]
collection.update_many.return_value = create_autospec(UpdateResult, matched_count=1)
NdarrayStore._concat_and_rewrite(self, collection, version, symbol, item, previous_version)
assert self.check_written.call_count == 1
示例4: test_prune_previous_versions_0_timeout
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_prune_previous_versions_0_timeout():
self = create_autospec(VersionStore, _versions=Mock())
self.name = sentinel.name
self._versions = create_autospec(Collection)
self._versions.with_options.return_value.find.__name__ = 'find'
self._versions.with_options.return_value.find.return_value = []
with patch('arctic.store.version_store.dt') as dt:
dt.utcnow.return_value = datetime.datetime(2013, 10, 1)
VersionStore._find_prunable_version_ids(self, sentinel.symbol, keep_mins=0)
assert self._versions.with_options.call_args_list == [call(read_preference=ReadPreference.PRIMARY)]
assert self._versions.with_options.return_value.find.call_args_list == [
call({'$or': [{'parent': {'$exists': False}},
{'parent': []}],
'symbol': sentinel.symbol,
'_id': {'$lt': bson.ObjectId('524a10810000000000000000')}},
sort=[('version', -1)],
skip=1,
projection={'FW_POINTERS_CONFIG': 1, '_id': 1, 'SEGMENT_SHAS': 1}
)]
示例5: test_read_handles_operation_failure
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_read_handles_operation_failure():
self = Mock(spec=VersionStore)
self._read_preference.return_value = sentinel.read_preference
self._collection = create_autospec(Collection)
self._read_metadata.side_effect = [sentinel.meta1, sentinel.meta2]
self._read_metadata.__name__ = 'name'
self._do_read.__name__ = 'name' # feh: mongo_retry decorator cares about this
self._do_read.side_effect = [OperationFailure('error'), sentinel.read]
VersionStore.read(self, sentinel.symbol, sentinel.as_of,
from_version=sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg)
# Assert that, for the two read calls, the second uses the new metadata
assert self._do_read.call_args_list == [call(sentinel.symbol, sentinel.meta1,
sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg,
read_preference=sentinel.read_preference)]
assert self._do_read_retry.call_args_list == [call(sentinel.symbol, sentinel.meta2,
sentinel.from_version,
date_range=sentinel.date_range,
other_kwarg=sentinel.other_kwarg,
read_preference=ReadPreference.PRIMARY)]
示例6: drop_collection
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def drop_collection(self, name_or_collection):
"""Drop a collection.
:Parameters:
- `name_or_collection`: the name of a collection to drop or the
collection object itself
"""
name = name_or_collection
if isinstance(name, Collection):
name = name.name
if not isinstance(name, str):
raise TypeError("name_or_collection must be an instance of "
"%s or Collection" % (str.__name__,))
self.__connection._purge_index(self.__name, name)
self.command("drop", str(name), allowable_errors=["ns not found"],
read_preference=ReadPreference.PRIMARY)
示例7: dereference
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def dereference(self, dbref, **kwargs):
"""Dereference a :class:`~bson.dbref.DBRef`, getting the
document it points to.
Raises :class:`TypeError` if `dbref` is not an instance of
:class:`~bson.dbref.DBRef`. Returns a document, or ``None`` if
the reference does not point to a valid document. Raises
:class:`ValueError` if `dbref` has a database specified that
is different from the current database.
:Parameters:
- `dbref`: the reference
- `**kwargs` (optional): any additional keyword arguments
are the same as the arguments to
:meth:`~pymongo.collection.Collection.find`.
"""
if not isinstance(dbref, DBRef):
raise TypeError("cannot dereference a %s" % type(dbref))
if dbref.database is not None and dbref.database != self.__name:
raise ValueError("trying to dereference a DBRef that points to "
"another database (%r not %r)" % (dbref.database,
self.__name))
return self[dbref.collection].find_one({"_id": dbref.id}, **kwargs)
示例8: restore_pymongo
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def restore_pymongo():
"""Restores pymongo"""
try:
from pymongo.collection import Collection
except ImportError: # pragma: no cover
pass
else:
for class_method in (
"bulk_write",
"delete_many",
"delete_one",
"insert_many",
"insert_one",
"replace_one",
"update_many",
"update_one",
):
if hasattr(getattr(Collection, class_method), "__wrapped__"):
setattr(
Collection,
class_method,
getattr(Collection, class_method).__wrapped__,
)
示例9: load_mongo_col
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def load_mongo_col(col: Collection) -> dict:
"""Load the pymongo collection to a dictionary.
In the dictionary. The key will be the '_id' and in each value which is a dictionary there will also be a
key '_id' so that the structure will be the same as the filesystem collection.
Parameters
----------
col : Collection
The mongodb collection.
Returns
-------
dct : dict
A dictionary with all the info in the collection.
"""
return {
doc['_id']: doc for doc in col.find({})
}
示例10: ensure_installed
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def ensure_installed():
global have_patched_collection
logger.debug("Instrumenting pymongo.")
if Collection is None:
logger.debug("Couldn't import pymongo.Collection - probably not installed.")
elif not have_patched_collection:
for name in COLLECTION_METHODS:
try:
setattr(
Collection, name, wrap_collection_method(getattr(Collection, name))
)
except Exception as exc:
logger.warning(
"Failed to instrument pymongo.Collection.%s: %r",
name,
exc,
exc_info=exc,
)
have_patched_collection = True
示例11: test_mongo_date_range_query_asserts
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_mongo_date_range_query_asserts():
self = create_autospec(TickStore)
self._collection = create_autospec(Collection)
self._collection.find_one.return_value = {'s': sentinel.start}
with pytest.raises(AssertionError):
TickStore._mongo_date_range_query(self, 'sym', DateRange(None, None, CLOSED_OPEN))
with pytest.raises(AssertionError):
TickStore._mongo_date_range_query(self, 'sym', DateRange(dt(2014, 1, 1), None))
with pytest.raises(AssertionError):
TickStore._mongo_date_range_query(self, 'sym', DateRange(None, dt(2014, 1, 1)))
示例12: test_concat_and_rewrite_checks_different_id
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_concat_and_rewrite_checks_different_id():
self = create_autospec(NdarrayStore)
collection = create_autospec(Collection)
version = {'_id': sentinel.version_id,
'segment_count': 1}
previous_version = {'_id': sentinel.id,
'up_to': sentinel.up_to,
'base_version_id': sentinel.base_version_id,
'version': sentinel.version,
'segment_count' : 5,
'append_count' : 3}
symbol = sentinel.symbol
item = []
collection.find.side_effect = [
[{'_id': sentinel.id, 'segment' : 47, 'compressed': True},
{'_id': sentinel.id_3, 'segment': 48, 'compressed': True},
{'_id': sentinel.id_4, 'segment': 49, 'compressed': False},
{'_id': sentinel.id_5, 'segment': 50, 'compressed': False},
{'_id': sentinel.id_6, 'segment': 51, 'compressed': False}], # 3 appended items
[{'_id': sentinel.id_2}] # the returned id is different after the update_many
]
expected_verify_find_spec = {'symbol': sentinel.symbol, 'segment': {'$lte': 47}, 'parent': sentinel.version_id}
collection.update_many.return_value = create_autospec(UpdateResult, matched_count=0)
with pytest.raises(DataIntegrityException) as e:
NdarrayStore._concat_and_rewrite(self, collection, version, symbol, item, previous_version)
assert collection.find.call_args_list[1] == call(expected_verify_find_spec)
assert str(e.value) == 'Symbol: sentinel.symbol:sentinel.version update_many updated 0 segments instead of 1'
示例13: test_read_reports_random_errors
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_read_reports_random_errors():
self = create_autospec(VersionStore, _versions=Mock(), _arctic_lib=Mock(),
_allow_secondary=True)
self._collection = create_autospec(Collection)
self._do_read.__name__ = 'name' # feh: mongo_retry decorator cares about this
self._do_read.side_effect = Exception('bad')
with pytest.raises(Exception) as e:
with patch('arctic.store.version_store.log_exception') as le:
VersionStore.read(self, sentinel.symbol, sentinel.as_of, sentinel.from_version)
assert 'bad' in str(e.value)
assert le.call_count == 1
示例14: test_find
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_find():
arctic_lib = create_autospec(ArcticLibraryBinding, instance=True)
collection = create_autospec(Collection, instance=True)
collection.find.return_value = (doc for doc in [sentinel.document])
arctic_lib.get_top_level_collection.return_value = collection
bsons = BSONStore(arctic_lib)
assert list(bsons.find(sentinel.filter)) == [sentinel.document]
assert collection.find.call_count == 1
assert collection.find.call_args_list == [call(sentinel.filter)]
示例15: test_find_one
# 需要导入模块: from pymongo import collection [as 别名]
# 或者: from pymongo.collection import Collection [as 别名]
def test_find_one():
arctic_lib = create_autospec(ArcticLibraryBinding, instance=True)
collection = create_autospec(Collection, instance=True)
collection.find_one.return_value = sentinel.document
arctic_lib.get_top_level_collection.return_value = collection
ms = BSONStore(arctic_lib)
assert ms.find_one(sentinel.filter) == sentinel.document
assert collection.find_one.call_count == 1
assert collection.find_one.call_args_list == [call(sentinel.filter)]