本文整理汇总了Python中pymongo.database.Database.get_collection方法的典型用法代码示例。如果您正苦于以下问题:Python Database.get_collection方法的具体用法?Python Database.get_collection怎么用?Python Database.get_collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.database.Database
的用法示例。
在下文中一共展示了Database.get_collection方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MongoopTrigger
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import get_collection [as 别名]
class MongoopTrigger(BaseTrigger):
def __init__(self, *args, **kwargs):
super(MongoopTrigger, self).__init__(*args, **kwargs)
database = self.params.get('database', 'mongoop')
collection = self.params.get('collection', 'history')
self.db = Database(self.mongoop.conn, database)
self.collection = self.db.get_collection(collection)
self.collection.create_index([('opid', DESCENDING)],
unique=True,
background=True)
def op_nok(self, operations):
try:
if operations:
self.collection.insert_many(operations)
except Exception as e:
logging.error('unable to bulk operations :: {} :: {}'.format(
self.name, e))
return False
else:
logging.info('run :: {} :: bulk insert {} operations'.format(
self.name, len(operations)))
return True
示例2: test_get_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import get_collection [as 别名]
def test_get_collection(self):
db = Database(self.client, "pymongo_test")
codec_options = CodecOptions(
tz_aware=True, uuid_representation=JAVA_LEGACY)
write_concern = WriteConcern(w=2, j=True)
coll = db.get_collection(
'foo', codec_options, ReadPreference.SECONDARY, write_concern)
self.assertEqual('foo', coll.name)
self.assertEqual(codec_options, coll.codec_options)
self.assertEqual(JAVA_LEGACY, coll.uuid_subtype)
self.assertEqual(ReadPreference.SECONDARY, coll.read_preference)
self.assertEqual(write_concern.document, coll.write_concern)
pref = Secondary([{"dc": "sf"}])
coll = db.get_collection('foo', read_preference=pref)
self.assertEqual(pref.mode, coll.read_preference)
self.assertEqual(pref.tag_sets, coll.tag_sets)
self.assertEqual(db.codec_options, coll.codec_options)
self.assertEqual(db.uuid_subtype, coll.uuid_subtype)
self.assertEqual(db.write_concern, coll.write_concern)
示例3: __init__
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import get_collection [as 别名]
def __init__(self, mongo_serialization_factory: MongoSerializationFactory, db: Database):
self.mongo_serialization_factory = mongo_serialization_factory
self.mongo_serialization = self.mongo_serialization_factory.get_instance()
self.db = db
self.coll = db.get_collection(UserDaoImpl.COLLECTION_NAME)
self.create_index()
示例4: Mongoop
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import get_collection [as 别名]
#.........这里部分代码省略.........
for t_name, t_values in self.op_triggers.items():
_callable = self._get_trigger_callable(t_name, t_values)
if _callable:
self.cycle_op_triggers.append(_callable)
for t_name, t_values in self.balancer_triggers.items():
_callable = self._get_trigger_callable(t_name, t_values, category='balancer')
if _callable:
self.cycle_balancer_triggers.append(_callable)
except TypeError as e:
logging.error('unable to authenticate to admin database :: {}'.format(e))
exit(1)
except OperationFailure as e:
logging.error('authentication failure :: {}'.format(e))
except ConnectionFailure as e:
logging.error('unable to connect to database :: {}'.format(e))
else:
logging.info('start mongoop :: {}'.format(self))
def __str__(self):
return u'{} :: frequency={} :: slow_query={} :: op_triggers={} :: balancer_triggers={}'.format(
self.conn, self._frequency, self._base_op_query, len(self.cycle_op_triggers),
len(self.cycle_balancer_triggers))
def __call__(self):
""" Main function.
"""
while True:
start = time()
self.call_op_triggers()
self.call_balancer_triggers()
exec_time = time() - start
if exec_time < self._frequency:
sleep(self._frequency - exec_time)
def call_op_triggers(self):
""" Main function to run the op triggers.
"""
operations = self._current_op()
for trigger in self.cycle_op_triggers:
trigger.run(operations=operations)
def call_balancer_triggers(self):
""" Main function to run the balancer triggers.
"""
if not self.balancer_triggers:
return True
balancer_state = self._get_balancer_state()
for trigger in self.cycle_balancer_triggers:
trigger.run(balancer_state=balancer_state)
def _get_trigger_callable(self, trigger_name, trigger_params, category='op'):
""" Retrieve the corresponding trigger by name and add into the triggers list.
Args:
"""
try:
trigger_module = import_module('mongoop.triggers.{}'.format(trigger_params['type']))
trigger_class = getattr(trigger_module, 'MongoopTrigger')
trigger = trigger_class(name=trigger_name, params=trigger_params,
mongoop=self, category=category)
except Exception as e:
logging.error('unable to retrieve the trigger callable :: {}'.format(e))
else:
return trigger
def _current_op(self):
""" Get informations on operations currently running.
"""
try:
op_inprog = {}
coll = self.db.get_collection("$cmd.sys.inprog")
result = coll.find_one(self._base_op_query)
op_inprog = result.get('inprog', {})
except Exception as e:
logging.error('unable to retrieve op :: {}'.format(e))
else:
if op_inprog:
logging.info('found {} slow op'.format(len(op_inprog)))
logging.debug('found {} slow op'.format(len(op_inprog)))
finally:
return op_inprog
def _get_balancer_state(self):
""" Return the balancer state.
Returns:
bool: True it's running, False otherwhise.
"""
try:
if self.conn.config.settings.find_one({'_id': 'balancer', 'stopped': True}):
logging.info('balancer state :: stopped')
return False
logging.info('balancer state :: started')
return True
except Exception as e:
logging.error('unable to get the balancer state :: {}'.format(e))