本文整理汇总了Python中pymongo.mongo_client.MongoClient.get_database方法的典型用法代码示例。如果您正苦于以下问题:Python MongoClient.get_database方法的具体用法?Python MongoClient.get_database怎么用?Python MongoClient.get_database使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.mongo_client.MongoClient
的用法示例。
在下文中一共展示了MongoClient.get_database方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: stash
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def stash(results):
"""
暂存到mongo数据库中。
"""
summary = {}
mongo = MongoClient(**config.mongo)
try:
for item_model, objs in results:
collection_name = item_model['name']
db = mongo.get_database('theforce')
collection = db.get_collection(collection_name)
collection.insert_many(objs)
summary[collection_name] = len(
objs) if collection_name not in summary else len(
objs) + summary[collection_name]
print
print "=" * 40
print ' ' * 15, u'Stash'
print "=" * 40
print
print u"数据已成功保存到MongoDB的theforce库中,其中新增数据:"
for name, length in summary.items():
print name, length
finally:
mongo.close()
示例2: mongo_text_test
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def mongo_text_test():
cli = MongoClient()
test = cli.get_database("test").get_collection("test_search")
test.create_index(
[("super_type", ASCENDING), ("resource_state", ASCENDING),
("uuid", TEXT), ("name", TEXT), ("description", TEXT)],
background=True
)
docs = [{
"uuid": str(uuid.uuid4()),
"name": str(i),
"description": "Nature, \"time, and patience are "
"the three great -physicians.",
"super_type": "super_vol",
"type": "vol",
"create_time": int(
time.time()),
"resource_state": "inUse"
} for i in range(1, 11)]
test.insert_many(docs)
text = [" -physicians"]
print test.find({
"description": {
"$regex": "|".join([re.sub(
r"(\*|\.|\?|\+|\$|\^|\[|\]|\(|\)|\{|\}|\||\\|/)",
r"\\\1",
g
) for g in text]),
"$options": "i"
}
}).count()
示例3: test_read_with_failover
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_read_with_failover(self):
c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
wait_until(lambda: c.primary, "discover primary")
wait_until(lambda: len(c.secondaries) == 2, "discover secondaries")
def iter_cursor(cursor):
for _ in cursor:
pass
return True
w = len(c.secondaries) + 1
db = c.get_database("pymongo_test",
write_concern=WriteConcern(w=w))
db.test.delete_many({})
# Force replication
db.test.insert_many([{'foo': i} for i in xrange(10)])
self.assertEqual(10, db.test.count())
db.read_preference = SECONDARY_PREFERRED
cursor = db.test.find().batch_size(5)
next(cursor)
self.assertEqual(5, cursor._Cursor__retrieved)
self.assertTrue(cursor.address in c.secondaries)
ha_tools.kill_primary()
# Primary failure shouldn't interrupt the cursor
self.assertTrue(iter_cursor(cursor))
self.assertEqual(10, cursor._Cursor__retrieved)
示例4: __init__
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def __init__(self):
connection = MongoClient(
settings['MONGO_SERVER'],
settings['MONGO_PORT']
)
db = connection.get_database(settings['MONGO_DB'])
self.collection = db[settings['MONGO_COLLECTION']]
示例5: test_get_database
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_get_database(self):
client = MongoClient(host, port, _connect=False)
codec_options = CodecOptions(
tz_aware=True, uuid_representation=JAVA_LEGACY)
write_concern = WriteConcern(w=2, j=True)
db = client.get_database(
'foo', codec_options, ReadPreference.SECONDARY, write_concern)
self.assertEqual('foo', db.name)
self.assertEqual(codec_options, db.codec_options)
self.assertEqual(JAVA_LEGACY, db.uuid_subtype)
self.assertEqual(ReadPreference.SECONDARY, db.read_preference)
self.assertEqual([{}], db.tag_sets)
self.assertEqual(write_concern.document, db.write_concern)
pref = Secondary([{"dc": "sf"}])
db = client.get_database('foo', read_preference=pref)
self.assertEqual(pref.mode, db.read_preference)
self.assertEqual(pref.tag_sets, db.tag_sets)
self.assertEqual({}, db.write_concern)
self.assertEqual(CodecOptions(), db.codec_options)
self.assertEqual(PYTHON_LEGACY, db.uuid_subtype)
示例6: test_last_error
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_last_error(self):
c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
wait_until(lambda: c.primary, "discover primary")
wait_until(lambda: c.secondaries, "discover secondary")
ha_tools.stepdown_primary()
db = c.get_database(
"pymongo_test", write_concern=WriteConcern(w=0))
db.test.insert_one({})
response = db.error()
self.assertTrue('err' in response and 'not master' in response['err'])
wait_until(lambda: len(c.secondaries) == 2, "discover two secondaries")
示例7: save
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def save(metas, batch_num=100):
"""
读取配置,把Mongo数据同步到mysql中。
"""
mongo = MongoClient(**config.mongo)
db = MySQLdb.connect(**config.mysql)
cursor = db.cursor()
print
print "=" * 40
print ' ' * 15, u'Mongo --> MySQL'
print "=" * 40
print
try:
mongo_db = mongo.get_database('theforce')
for meta in metas:
for model_name, item_model in meta.iter_model():
collection_name = item_model['name']
table_name = item_model['table']
attrs = meta.get_model_persist_attr_names(item_model)
collection = mongo_db.get_collection(collection_name)
results = [obj for obj in collection.find({})]
sql = "insert into {0}({1}) values({2})".format(
table_name, ','.join(attrs),
','.join(itertools.repeat('%s', len(attrs))))
print
print '-' * 40
print u'开始处理{0}@mongo --> {1}@mysql, 共{2}条数据,每批{3}条批量迁移:'.format(
collection_name, table_name, len(results), batch_num)
# 分组进行批量处理
results2 = itertools.izip(itertools.count(), results)
for group_key, group_it in itertools.groupby(
results2, lambda item: item[0] / batch_num):
print '.',
values = [[obj[attr] for attr in attrs]
for index, obj in group_it]
cursor.executemany(sql, values)
print u'[完成]'
finally:
mongo.close()
cursor.close()
db.close()
示例8: test_writes_with_failover
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_writes_with_failover(self):
c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
wait_until(lambda: c.primary, "discover primary")
wait_until(lambda: len(c.secondaries) == 2, "discover secondaries")
primary = c.primary
w = len(c.secondaries) + 1
db = c.get_database("pymongo_test",
write_concern=WriteConcern(w=w))
db.test.delete_many({})
db.test.insert_one({'foo': 'bar'})
self.assertEqual('bar', db.test.find_one()['foo'])
killed = ha_tools.kill_primary(9)
self.assertTrue(bool(len(killed)))
# Wait past pool's check interval, so it throws an error from
# get_socket().
time.sleep(1)
# Verify that we only raise AutoReconnect, not some other error,
# while we wait for new primary.
for _ in xrange(10000):
try:
db.test.insert_one({'bar': 'baz'})
# No error, found primary.
break
except AutoReconnect:
time.sleep(.01)
else:
self.fail("Couldn't connect to new primary")
# Found new primary.
self.assertTrue(c.primary)
self.assertTrue(primary != c.primary)
self.assertEqual('baz', db.test.find_one({'bar': 'baz'})['bar'])
示例9: test_ship_of_theseus
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_ship_of_theseus(self):
c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
db = c.get_database(
"pymongo_test",
write_concern=WriteConcern(w=len(c.secondaries) + 1))
db.test.insert_one({})
find_one = db.test.find_one
primary = ha_tools.get_primary()
secondary1 = ha_tools.get_random_secondary()
new_hosts = []
for i in range(3):
new_hosts.append(ha_tools.add_member())
# RS closes all connections after reconfig.
for j in xrange(30):
try:
if ha_tools.get_primary():
break
except (ConnectionFailure, OperationFailure):
pass
time.sleep(1)
else:
self.fail("Couldn't recover from reconfig")
# Wait for new members to join.
for _ in xrange(120):
if ha_tools.get_primary() and len(ha_tools.get_secondaries()) == 4:
break
time.sleep(1)
else:
self.fail("New secondaries didn't join")
ha_tools.kill_members([primary, secondary1], 9)
time.sleep(5)
wait_until(lambda: (ha_tools.get_primary()
and len(ha_tools.get_secondaries()) == 2),
"fail over",
timeout=30)
time.sleep(2 * self.heartbeat_frequency)
# No error.
find_one()
find_one(read_preference=SECONDARY)
# All members down.
ha_tools.kill_members(new_hosts, 9)
self.assertRaises(
ConnectionFailure,
find_one, read_preference=SECONDARY)
ha_tools.restart_members(new_hosts)
# Should be able to reconnect to set even though original seed
# list is useless. Use SECONDARY so we don't have to wait for
# the election, merely for the client to detect members are up.
time.sleep(2 * self.heartbeat_frequency)
find_one(read_preference=SECONDARY)
# Kill new members and switch back to original two members.
ha_tools.kill_members(new_hosts, 9)
self.assertRaises(
ConnectionFailure,
find_one, read_preference=SECONDARY)
ha_tools.restart_members([primary, secondary1])
# Wait for members to figure out they're secondaries.
wait_until(lambda: len(ha_tools.get_secondaries()) == 2,
"detect two secondaries",
timeout=30)
# Should be able to reconnect to set again.
time.sleep(2 * self.heartbeat_frequency)
find_one(read_preference=SECONDARY)
示例10: test_secondary_connection
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
def test_secondary_connection(self):
self.c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
wait_until(lambda: len(self.c.secondaries), "discover secondary")
# Wait for replication...
w = len(self.c.secondaries) + 1
db = self.c.get_database("pymongo_test",
write_concern=WriteConcern(w=w))
db.test.delete_many({})
db.test.insert_one({'foo': 'bar'})
# Test direct connection to a primary or secondary
primary_host, primary_port = ha_tools.get_primary().split(':')
primary_port = int(primary_port)
(secondary_host,
secondary_port) = ha_tools.get_secondaries()[0].split(':')
secondary_port = int(secondary_port)
arbiter_host, arbiter_port = ha_tools.get_arbiters()[0].split(':')
arbiter_port = int(arbiter_port)
# MongoClient succeeds no matter the read preference
for kwargs in [
{'read_preference': PRIMARY},
{'read_preference': PRIMARY_PREFERRED},
{'read_preference': SECONDARY},
{'read_preference': SECONDARY_PREFERRED},
{'read_preference': NEAREST},
]:
client = MongoClient(
primary_host,
primary_port,
serverSelectionTimeoutMS=self.server_selection_timeout,
**kwargs)
wait_until(lambda: primary_host == client.host,
"connect to primary")
self.assertEqual(primary_port, client.port)
self.assertTrue(client.is_primary)
# Direct connection to primary can be queried with any read pref
self.assertTrue(client.pymongo_test.test.find_one())
client = MongoClient(
secondary_host,
secondary_port,
serverSelectionTimeoutMS=self.server_selection_timeout,
**kwargs)
wait_until(lambda: secondary_host == client.host,
"connect to secondary")
self.assertEqual(secondary_port, client.port)
self.assertFalse(client.is_primary)
# Direct connection to secondary can be queried with any read pref
# but PRIMARY
if kwargs.get('read_preference') != PRIMARY:
self.assertTrue(client.pymongo_test.test.find_one())
else:
self.assertRaises(
AutoReconnect, client.pymongo_test.test.find_one)
# Since an attempt at an acknowledged write to a secondary from a
# direct connection raises AutoReconnect('not master'), MongoClient
# should do the same for unacknowledged writes.
try:
client.get_database(
"pymongo_test",
write_concern=WriteConcern(w=0)).test.insert_one({})
except AutoReconnect as e:
self.assertEqual('not master', e.args[0])
else:
self.fail(
'Unacknowledged insert into secondary client %s should'
'have raised exception' % (client,))
# Test direct connection to an arbiter
client = MongoClient(
arbiter_host,
arbiter_port,
serverSelectionTimeoutMS=self.server_selection_timeout,
**kwargs)
wait_until(lambda: arbiter_host == client.host,
"connect to arbiter")
self.assertEqual(arbiter_port, client.port)
self.assertFalse(client.is_primary)
# See explanation above
try:
client.get_database(
"pymongo_test",
write_concern=WriteConcern(w=0)).test.insert_one({})
except AutoReconnect as e:
self.assertEqual('not master', e.args[0])
else:
self.fail(
'Unacknowledged insert into arbiter client %s should'
#.........这里部分代码省略.........
示例11: TestReadPreference
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import get_database [as 别名]
class TestReadPreference(HATestCase):
# Speed up assertReadFrom() when no server is suitable.
server_selection_timeout = 0.001
def setUp(self):
super(TestReadPreference, self).setUp()
members = [
# primary
{'tags': {'dc': 'ny', 'name': 'primary'}},
# secondary
{'tags': {'dc': 'la', 'name': 'secondary'}, 'priority': 0},
# other_secondary
{'tags': {'dc': 'ny', 'name': 'other_secondary'}, 'priority': 0},
]
res = ha_tools.start_replica_set(members)
self.seed, self.name = res
primary = ha_tools.get_primary()
self.primary = partition_node(primary)
self.primary_tags = ha_tools.get_tags(primary)
# Make sure priority worked
self.assertEqual('primary', self.primary_tags['name'])
self.primary_dc = {'dc': self.primary_tags['dc']}
secondaries = ha_tools.get_secondaries()
(secondary, ) = [
s for s in secondaries
if ha_tools.get_tags(s)['name'] == 'secondary']
self.secondary = partition_node(secondary)
self.secondary_tags = ha_tools.get_tags(secondary)
self.secondary_dc = {'dc': self.secondary_tags['dc']}
(other_secondary, ) = [
s for s in secondaries
if ha_tools.get_tags(s)['name'] == 'other_secondary']
self.other_secondary = partition_node(other_secondary)
self.other_secondary_tags = ha_tools.get_tags(other_secondary)
self.other_secondary_dc = {'dc': self.other_secondary_tags['dc']}
self.c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
self.w = len(self.c.secondaries) + 1
self.db = self.c.get_database("pymongo_test",
write_concern=WriteConcern(w=self.w))
self.db.test.delete_many({})
self.db.test.insert_many([{'foo': i} for i in xrange(10)])
self.clear_ping_times()
def set_ping_time(self, host, ping_time_seconds):
ServerDescription._host_to_round_trip_time[host] = ping_time_seconds
def clear_ping_times(self):
ServerDescription._host_to_round_trip_time.clear()
def test_read_preference(self):
# We pass through four states:
#
# 1. A primary and two secondaries
# 2. Primary down
# 3. Primary up, one secondary down
# 4. Primary up, all secondaries down
#
# For each state, we verify the behavior of PRIMARY,
# PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, and NEAREST
c = MongoClient(
self.seed,
replicaSet=self.name,
serverSelectionTimeoutMS=self.server_selection_timeout)
wait_until(lambda: c.primary, "discover primary")
wait_until(lambda: len(c.secondaries) == 2, "discover secondaries")
def assertReadFrom(member, *args, **kwargs):
utils.assertReadFrom(self, c, member, *args, **kwargs)
def assertReadFromAll(members, *args, **kwargs):
utils.assertReadFromAll(self, c, members, *args, **kwargs)
def unpartition_node(node):
host, port = node
return '%s:%s' % (host, port)
# To make the code terser, copy hosts into local scope
primary = self.primary
secondary = self.secondary
other_secondary = self.other_secondary
bad_tag = {'bad': 'tag'}
#.........这里部分代码省略.........