本文整理汇总了Python中pymongo.mongo_client.MongoClient.close方法的典型用法代码示例。如果您正苦于以下问题:Python MongoClient.close方法的具体用法?Python MongoClient.close怎么用?Python MongoClient.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.mongo_client.MongoClient
的用法示例。
在下文中一共展示了MongoClient.close方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: stash
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [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: test_ipv6
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def test_ipv6(self):
c = MongoClient("mongodb://[::1]:%d" % (port,), replicaSet=self.name)
# Client switches to IPv4 once it has first ismaster response.
msg = 'discovered primary with IPv4 address "%r"' % (self.primary,)
wait_until(lambda: c.primary == self.primary, msg)
# Same outcome with both IPv4 and IPv6 seeds.
c = MongoClient("[::1]:%d,localhost:%d" % (port, port),
replicaSet=self.name)
wait_until(lambda: c.primary == self.primary, msg)
if client_context.auth_enabled:
auth_str = "%s:%[email protected]" % (db_user, db_pwd)
else:
auth_str = ""
uri = "mongodb://%slocalhost:%d,[::1]:%d" % (auth_str, port, port)
client = MongoClient(uri, replicaSet=self.name)
client.pymongo_test.test.insert_one({"dummy": u("object")})
client.pymongo_test_bernie.test.insert_one({"dummy": u("object")})
dbs = client.database_names()
self.assertTrue("pymongo_test" in dbs)
self.assertTrue("pymongo_test_bernie" in dbs)
client.close()
示例3: doEverything
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def doEverything():
# certfile = '/home/bryan/Downloads/baratheon.pem'
conn = MongoClient(url)
db = conn[database]
commands = []
collectionName = "pythonMongo"
commands.append("Creating collection " + collectionName)
collection = db[collectionName]
#insert 1
commands.append("# 1 Inserts")
commands.append("# 1.1 Insert a single document to a collection")
collection.insert({"name": "test1", "value": 1})
commands.append("Inserted {\"name\": \"test1\", \"value\": 1}")
#insert many
commands.append("#1.2 Inserting multiple entries into collection")
multiPost = [{"name": "test1", "value": 1},{"name": "test2", "value": 2}, {"name": "test3", "value": 3}]
collection.insert(multiPost)
commands.append("Inserted \n {\"name\": \"test1\", \"value\": 1} \n {\"name\": \"test2\", \"value\": 2} \n {\"name\": \"test3\", \"value\": 3}")
# Find
commands.append("#2 Queries")
commands.append("#2.1 Find one that matches a query condition")
commands.append(collection.find_one({"name": "test1"}))
# Find all
commands.append("#2.2 Find all that match a query condition")
for doc in collection.find({"name": "test1"}):
commands.append(doc)
# Display all documents
commands.append( "#2.3 Find all documents in collection")
for doc in collection.find():
commands.append(doc)
# update document
commands.append("#3 Updating Documents")
collection.update({"name": "test3"}, {"$set": { "value": 4}})
commands.append("Updated test3 with value 4")
# delete document
commands.append("#4 Delete Documents")
collection.remove({"name": "test2"})
commands.append("Deleted all with name test2")
# Display all collection names
commands.append("#5 Get a list of all of the collections")
commands.append( db.collection_names())
commands.append("#6 Drop a collection")
db.drop_collection(collectionName)
conn.close()
commands.append("Connection to database has been closed")
return commands
示例4: test_disconnect
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def test_disconnect(self):
c = MongoClient(host, port)
coll = c.pymongo_test.bar
c.close()
c.close()
coll.count()
c.close()
c.close()
coll.count()
示例5: test_properties
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def test_properties(self):
c = client_context.rs_client
c.admin.command('ping')
wait_until(lambda: c.primary == self.primary, "discover primary")
wait_until(lambda: c.arbiters == self.arbiters, "discover arbiters")
wait_until(lambda: c.secondaries == self.secondaries,
"discover secondaries")
self.assertEqual(c.primary, self.primary)
self.assertEqual(c.secondaries, self.secondaries)
self.assertEqual(c.arbiters, self.arbiters)
self.assertEqual(c.max_pool_size, 100)
# Make sure MongoClient's properties are copied to Database and
# Collection.
for obj in c, c.pymongo_test, c.pymongo_test.test:
self.assertEqual(obj.codec_options, CodecOptions())
self.assertEqual(obj.read_preference, ReadPreference.PRIMARY)
self.assertEqual(obj.write_concern, WriteConcern())
cursor = c.pymongo_test.test.find()
self.assertEqual(
ReadPreference.PRIMARY, cursor._Cursor__read_preference)
tag_sets = [{'dc': 'la', 'rack': '2'}, {'foo': 'bar'}]
secondary = Secondary(tag_sets=tag_sets)
c = MongoClient(
pair, replicaSet=self.name, maxPoolSize=25,
document_class=SON, tz_aware=True,
read_preference=secondary,
localThresholdMS=77, j=True)
self.assertEqual(c.max_pool_size, 25)
for obj in c, c.pymongo_test, c.pymongo_test.test:
self.assertEqual(obj.codec_options, CodecOptions(SON, True))
self.assertEqual(obj.read_preference, secondary)
self.assertEqual(obj.write_concern, WriteConcern(j=True))
cursor = c.pymongo_test.test.find()
self.assertEqual(
secondary, cursor._Cursor__read_preference)
nearest = Nearest(tag_sets=[{'dc': 'ny'}, {}])
cursor = c.pymongo_test.get_collection(
"test", read_preference=nearest).find()
self.assertEqual(nearest, cursor._Cursor__read_preference)
self.assertEqual(c.max_bson_size, 16777216)
c.close()
示例6: save
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [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()
示例7: test_kill_cursors_warning
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def test_kill_cursors_warning(self):
# If kill_cursors is called while the client is disconnected, it
# can't risk taking the lock to reconnect, in case it's being called
# from Cursor.__del__, see PYTHON-799. Test that it shows a warning
# in this case.
client = MongoClient(host, port)
collection = client.pymongo_test.test
collection.insert({} for _ in range(4))
cursor = collection.find().batch_size(1)
cursor.next()
client.close()
ctx = catch_warnings()
try:
warnings.simplefilter("error", UserWarning)
self.assertRaises(UserWarning, cursor.close)
finally:
ctx.exit()
# Reconnect.
collection.find_one()
cursor.close()
示例8: GridFSOperations
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
class GridFSOperations(Operations):
def __init__(self, host, db_name='test', collection_name='fs'):
self.client = MongoClient(host)
self.db = Database(self.client, db_name)
self.fs = GridFS(self.db, collection_name)
def _new_file(self, name):
return self.fs.new_file(
filename=name,
aliases=[],
length=0,
upload_date=datetime.now())
@logmethod
def init(self):
pass
@logmethod
def access(self, inode, mode, ctx):
return True
@logmethod
def getattr(self, inode):
if inode == 1:
return Operations.getattr(self, inode)
else:
return grid2attrs(self.fs.get(int2oid(inode)))
@logmethod
def lookup(self, parent_inode, name):
if parent_inode != 1:
raise FUSEError(errno.ENOENT)
try:
gridout = self.fs.get_last_version(filename=name.decode())
except NoFile:
raise FUSEError(errno.ENOENT)
return grid2attrs(gridout)
@logmethod
def create(self, inode_parent, name, mode, flags, ctx):
gridin = self._new_file(name.decode())
fh = oid2int(gridin._id)
grid_cache[fh] = gridin
return (fh, grid2attrs(gridin))
@logmethod
def flush(self, fh):
grid = grid_cache[fh]
grid.close()
@logmethod
def setattr(self, inode, attr):
gridout = self.fs.get(int2oid(inode))
return grid2attrs(gridout)
@logmethod
def release(self, fh):
del grid_cache[fh]
@logmethod
def forget(self, inode_list):
for inode in inode_list:
if inode in oid_cache.ints:
del oid_cache.ints[inode]
@logmethod
def destroy(self):
self.client.close()
@logmethod
def open(self, inode, flags):
gridout = self.fs.get(int2oid(inode))
grid_cache[inode] = gridout
return inode
@logmethod
def read(self, fh, off, size):
grid = grid_cache[fh]
if isinstance(grid, GridIn):
grid.close()
grid = self.fs.get(int2oid(fh))
grid_cache[fh] = grid
grid.seek(off)
return grid.read(size)
@logmethod
def write(self, fh, off, buf):
grid = grid_cache[fh]
if isinstance(grid, GridOut):
offbuf = grid.read(off)
grid = self._new_file(name=grid.name)
grid_cache[fh] = grid
#.........这里部分代码省略.........
示例9: doEverything
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
def doEverything():
# Get database connectivity information
database, url = getDatabaseInfo()
# Run test
try:
client = MongoClient(url)
db = client[database]
output = []
output.append("Starting database test.... ")
collectionName = "pythonMongo"
output.append("Creating collection " + collectionName)
collection = db[collectionName]
#insert 1
output.append("# 1 Inserts")
output.append("# 1.1 Insert a single document to a collection")
collection.insert({"name": "test1", "value": 1})
output.append("Inserted {\"name\": \"test1\", \"value\": 1}")
#insert many
output.append("#1.2 Inserting multiple entries into collection")
multiPost = [{"name": "test1", "value": 1},{"name": "test2", "value": 2}, {"name": "test3", "value": 3}]
collection.insert(multiPost)
output.append("Inserted \n {\"name\": \"test1\", \"value\": 1} \n {\"name\": \"test2\", \"value\": 2} \n {\"name\": \"test3\", \"value\": 3}")
# Find
output.append("#2 Queries")
output.append("#2.1 Find one that matches a query condition")
output.append(collection.find_one({"name": "test1"}))
# Find all
output.append("#2.2 Find all that match a query condition")
for doc in collection.find({"name": "test1"}):
output.append(doc)
# Display all documents
output.append( "#2.3 Find all documents in collection")
for doc in collection.find():
output.append(doc)
# update document
output.append("#3 Updating Documents")
collection.update({"name": "test3"}, {"$set": { "value": 4}})
output.append("Updated test3 with value 4")
# delete document
output.append("#4 Delete Documents")
collection.remove({"name": "test2"})
output.append("Deleted all with name test2")
# Display all collection names
output.append("#5 Get a list of all of the collections")
output.append( db.collection_names())
output.append("#6 Drop a collection")
db.drop_collection(collectionName)
except Exception as e:
logging.exception(e)
output.append("EXCEPTION (see log for details): " + str(e))
finally:
if client is not None:
client.close()
output.append("Connection to database has been closed")
return output
示例10: DBServer
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
class DBServer():
def __init__(self, collection_name, table):
self.collection_name = collection_name
self.table = table
cfg = DBUtils.read_config()
url = "mongodb://{0}:{1}/".format(cfg.host, cfg.port)
logger.debug("The db url", url)
self._client = MongoClient(url)
# can not connect to any db except admin server
username, password = getattr(cfg, 'username', ''), getattr(cfg, 'password', '')
if not (len(username) <= 0 or len(password) <= 0):
self._client.admin.authenticate(cfg.username, cfg.password)
db = self._client[collection_name]
self.table = db[table]
@log
def query(self, pattern=None, **condition):
"""
:param pattern: dict pattern suitable
:param condition: condition to pagination
:return:suitable information
"""
if pattern is None:
# if pattern is None,the conditions must be empty
values = self.table.find().limit(20)
return convert_list(values)
if condition is None or len(condition) <= 0:
# if condition is empty
values = self.table.find(pattern)
return convert_list(values)
dcu = DefaultConditionUtil(**condition)
if dcu.get_complex_expression() is not None:
pattern = pattern.update(**dcu.get_complex_expression())
ret = self.table.find(pattern).skip(dcu.get_offset()).limit(dcu.get_limit()).sort(dcu.sort_style())
return convert_list(ret)
@log
def find_one(self, **condition):
one = self.table.find_one(condition)
return convert_dict(one)
@log
def find_all(self):
"""
:return: all information
"""
values = self.table.find()
return convert_list(values)
@log
def insert(self, obj):
self.table.insert(obj)
@log
def delete(self, pattern=None, **condition):
"""
:param id: remove obj id
:param pattern: pattern data
:param condition:suitable data
:return:nothing
"""
if pattern is None:
return
self.table.remove(pattern)
@log
def update(self, pattern=None, **new):
"""
:param id: update id
:param other: condition
:param new: new data
:return:
"""
self.table.update(pattern, new)
@log
def query_by_complex_condition(self, code):
"""
:param code: query information via complex condition use javascript code
:return:
"""
values = self.table.find().where(code)
return convert_list(values)
@log
def count(self, id=None, **conditions):
if id is None and conditions is None:
return {'count': self.table.find().count()}
if conditions is None:
return {'count': self.table.find(dict(id=id)).count()}
if id is None:
return {'count': self.table.find(conditions).count()}
conditions['id'] = id
return {'count': self.table.find(conditions)}
@log
def close(self):
self._client.close()
def __del__(self):
#.........这里部分代码省略.........
示例11: Mongo
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
class Mongo(DatabaseManager):
def __init__(self,options={}):
self.config = {
'HOST':'localhost',
'PORT':27017,
'COLLECTION': 'default'
}
self.config.update(options)
print self.config
self.connect()
def connect(self):
self.client = MongoClient(
self.config['HOST'],
self.config['PORT']
)
self.db = self.client[self.config['COLLECTION']]
def iter(self):
return self.db.collection.find()
def close(self):
if self.client:
self.client.close()
def count(self):
return self.db.collection.count()
def put(self,data):
if isinstance(data, types.ListType):
# For bulk insert, inject timestamp
for x in data:
x[TIMESTAMP_CREATED] = datetime.datetime.utcnow()
else:
# For single insert, inject timestamp
data[TIMESTAMP_CREATED] = datetime.datetime.utcnow()
try:
self.db.collection.insert(data)
return True
except Exception as e:
print e
return False
def update(self):
pass
def get(self, query=False):
if not query:
# Get all documents in collection
return list(self.db.collection.find())
def delete(self):
pass
示例12: doEverything
# 需要导入模块: from pymongo.mongo_client import MongoClient [as 别名]
# 或者: from pymongo.mongo_client.MongoClient import close [as 别名]
#.........这里部分代码省略.........
for doc in sys.find(joinTableCollection):
output.append(doc)
output.append("#2.7c Join table-table")
joinTableTable= { "$collections" : { cityTableName : { "$project" : { "name" : 1 , "population" : 1 , "longitude" : 1 , "latitude" : 1}} ,
codeTableName : { "$project" : { "countryCode" : 1 , "countryName" : 1}}} ,
"$condition" : { "cityTable.countryCode": "codeTable.countryCode"}}
for doc in sys.find(joinTableTable):
output.append(doc)
output.append("#2.8 Changed Batch Size")
# docs = collection.find().batch_size(2)
# for doc in docs:
# output.append(doc)
output.append("#2.9 Projection clause")
output.append("Displaying results without longitude and latitude:")
for doc in collection.find({"countryCode" : 1}, {"longitude":0, "latitude" : 0}):
output.append(doc)
# update document
output.append("\n#3 Update Documents")
collection.update({"name": seattle.name}, {"$set": { "countryCode": 999}})
output.append("Updated %s with countryCode 999" % seattle.name)
# delete document
output.append("\n#4 Delete Documents")
collection.remove({"name": tokyo.name})
output.append("Deleted all with name %s" % tokyo.name)
# Display all collection names
output.append("\n#5 Get a list of all of the collections")
output.append( db.collection_names())
#SQL Passthrough
output.append("\n#6 SQL passthrough")
sql = db["system.sql"]
query = {"$sql": "create table town (name varchar(255), countryCode int)"}
for doc in sql.find(query):
output.append(doc)
query = {"$sql": "insert into town values ('Lawrence', 1)"}
for doc in sql.find(query):
output.append(doc)
query = {"$sql": "drop table town"}
for doc in sql.find(query):
output.append(doc)
#Transactions
output.append("\n#7 Transactions")
db.command({"transaction": "enable"})
collection.insert(sydney.toJSON())
db.command({"transaction": "commit"})
collection.insert(melbourne.toJSON())
db.command({"transaction": "rollback"})
db.command({"transaction": "disable"})
for doc in collection.find():
output.append(doc)
output.append("\n#8 output")
output.append("#8.1 Count")
count = db.command("count", collectionName)
output.append("There are %d documents in the collection" % count['n'])
output.append("#8.2 Distinct")
distinct = db.command("distinct", collectionName, key="countryCode")
output.append("The distinct country codes are %s" % distinct['values'])
output.append("#8.3 collection names ")
output.append(db.collection_names())
output.append("#8.3 Database stats")
output.append(db.command("dbstats"))
output.append("#8.4 Collection stats")
output.append(db.command("collstats", collectionName))
output.append("\n#9 Drop a collection")
db.drop_collection(collectionName)
db.drop_collection(joinCollectionName)
db.drop_collection(cityTableName)
db.drop_collection(codeTableName)
except Exception as e:
logging.exception(e)
output.append("EXCEPTION (see log for details): " + str(e))
finally:
if client is not None:
client.close()
output.append("Connection to database has been closed")
return output