本文整理汇总了Python中pymongo.database.Database.collection_names方法的典型用法代码示例。如果您正苦于以下问题:Python Database.collection_names方法的具体用法?Python Database.collection_names怎么用?Python Database.collection_names使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.database.Database
的用法示例。
在下文中一共展示了Database.collection_names方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_create_collection(self):
db = Database(self.client, "pymongo_test")
db.test.insert({"hello": "world"})
self.assertRaises(CollectionInvalid, db.create_collection, "test")
db.drop_collection("test")
self.assertRaises(TypeError, db.create_collection, 5)
self.assertRaises(TypeError, db.create_collection, None)
self.assertRaises(InvalidName, db.create_collection, "coll..ection")
test = db.create_collection("test")
test.save({"hello": u"world"})
self.assertEqual(db.test.find_one()["hello"], "world")
self.assertTrue(u"test" in db.collection_names())
db.drop_collection("test.foo")
db.create_collection("test.foo")
self.assertTrue(u"test.foo" in db.collection_names())
expected = {}
if version.at_least(self.client, (2, 7, 0)):
# usePowerOf2Sizes server default
expected["flags"] = 1
result = db.test.foo.options()
# mongos 2.2.x adds an $auth field when auth is enabled.
result.pop("$auth", None)
self.assertEqual(result, expected)
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
示例2: test_drop_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_drop_collection(self):
db = Database(self.client, "pymongo_test")
self.assertRaises(TypeError, db.drop_collection, 5)
self.assertRaises(TypeError, db.drop_collection, None)
db.test.insert_one({"dummy": u("object")})
self.assertTrue("test" in db.collection_names())
db.drop_collection("test")
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u("object")})
self.assertTrue("test" in db.collection_names())
db.drop_collection(u("test"))
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u("object")})
self.assertTrue("test" in db.collection_names())
db.drop_collection(db.test)
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u("object")})
self.assertTrue("test" in db.collection_names())
db.test.drop()
self.assertFalse("test" in db.collection_names())
db.test.drop()
db.drop_collection(db.test.doesnotexist)
示例3: test_drop_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_drop_collection(self):
db = Database(self.connection, "pymongo_test")
self.assertRaises(TypeError, db.drop_collection, 5)
self.assertRaises(TypeError, db.drop_collection, None)
db.test.save({"dummy": u"object"})
self.assert_("test" in db.collection_names())
db.drop_collection("test")
self.assertFalse("test" in db.collection_names())
db.test.save({"dummy": u"object"})
self.assert_("test" in db.collection_names())
db.drop_collection(u"test")
self.assertFalse("test" in db.collection_names())
db.test.save({"dummy": u"object"})
self.assert_("test" in db.collection_names())
db.drop_collection(db.test)
self.assertFalse("test" in db.collection_names())
db.test.save({"dummy": u"object"})
self.assert_("test" in db.collection_names())
db.test.drop()
self.assertFalse("test" in db.collection_names())
db.test.drop()
db.drop_collection(db.test.doesnotexist)
示例4: test_collection_names
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_collection_names(self):
db = Database(self.client, "pymongo_test")
db.test.save({"dummy": u"object"})
db.test.mike.save({"dummy": u"object"})
colls = db.collection_names()
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
for coll in colls:
self.assertTrue("$" not in coll)
colls_without_systems = db.collection_names(False)
for coll in colls_without_systems:
self.assertTrue(not coll.startswith("system."))
示例5: test_drop_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_drop_collection(self):
db = Database(self.client, "pymongo_test")
self.assertRaises(TypeError, db.drop_collection, 5)
self.assertRaises(TypeError, db.drop_collection, None)
db.test.insert_one({"dummy": u"object"})
self.assertTrue("test" in db.collection_names())
db.drop_collection("test")
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u"object"})
self.assertTrue("test" in db.collection_names())
db.drop_collection(u"test")
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u"object"})
self.assertTrue("test" in db.collection_names())
db.drop_collection(db.test)
self.assertFalse("test" in db.collection_names())
db.test.insert_one({"dummy": u"object"})
self.assertTrue("test" in db.collection_names())
db.test.drop()
self.assertFalse("test" in db.collection_names())
db.test.drop()
db.drop_collection(db.test.doesnotexist)
if client_context.version.at_least(3, 3, 9) and client_context.is_rs:
db_wc = Database(self.client, 'pymongo_test',
write_concern=IMPOSSIBLE_WRITE_CONCERN)
with self.assertRaises(WriteConcernError):
db_wc.drop_collection('test')
示例6: test_collection_names
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_collection_names(self):
db = Database(self.connection, "pymongo_test")
db.test.save({"dummy": u"object"})
db.test.mike.save({"dummy": u"object"})
colls = db.collection_names()
self.assert_("test" in colls)
self.assert_("test.mike" in colls)
for coll in colls:
self.assert_("$" not in coll)
示例7: test_create_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_create_collection(self):
db = Database(self.client, "pymongo_test")
db.test.insert_one({"hello": "world"})
self.assertRaises(CollectionInvalid, db.create_collection, "test")
db.drop_collection("test")
self.assertRaises(TypeError, db.create_collection, 5)
self.assertRaises(TypeError, db.create_collection, None)
self.assertRaises(InvalidName, db.create_collection, "coll..ection")
test = db.create_collection("test")
self.assertTrue(u("test") in db.collection_names())
test.insert_one({"hello": u("world")})
self.assertEqual(db.test.find_one()["hello"], "world")
db.drop_collection("test.foo")
db.create_collection("test.foo")
self.assertTrue(u("test.foo") in db.collection_names())
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
示例8: test_create_collection
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_create_collection(self):
db = Database(self.connection, "pymongo_test")
db.test.insert({"hello": "world"})
self.assertRaises(CollectionInvalid, db.create_collection, "test")
db.drop_collection("test")
self.assertRaises(TypeError, db.create_collection, 5)
self.assertRaises(TypeError, db.create_collection, None)
self.assertRaises(InvalidName, db.create_collection, "coll..ection")
test = db.create_collection("test")
test.save({"hello": u"world"})
self.assertEqual(db.test.find_one()["hello"], "world")
self.assert_(u"test" in db.collection_names())
db.drop_collection("test.foo")
db.create_collection("test.foo")
self.assert_(u"test.foo" in db.collection_names())
self.assertEqual(db.test.foo.options(), {})
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
示例9: test_collection_names
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def test_collection_names(self):
db = Database(self.client, "pymongo_test")
db.test.insert_one({"dummy": u("object")})
db.test.mike.insert_one({"dummy": u("object")})
colls = db.collection_names()
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
for coll in colls:
self.assertTrue("$" not in coll)
colls_without_systems = db.collection_names(False)
for coll in colls_without_systems:
self.assertTrue(not coll.startswith("system."))
# Force more than one batch.
db = self.client.many_collections
for i in range(101):
db["coll" + str(i)].insert_one({})
# No Error
try:
db.collection_names()
finally:
self.client.drop_database("many_collections")
示例10: borrarindicesevitardups
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def borrarindicesevitardups():
# Conexion a local
client = MongoClient()
try:
# The ismaster command is cheap and does not require auth.
client.admin.command('ismaster')
db = Database(client=client,
name=ConfiguracionGlobal.MONGODB)
except ConnectionFailure:
print("Server not available")
# Limpiado tablas e indices con pymongo
for collname in db.collection_names():
coll = Collection(db, name=collname)
coll.drop_indexes()
client.close()
示例11: conn
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def conn(self, line):
"""Conenct to mongo database in ipython shell.
Examples::
%mongo_connect
%mongo_connect mongodb://hostname:port
"""
if line:
uri = line
else:
uri = 'mongodb://127.0.0.1:27017'
self._conn = MongoClient(uri)
# add db and collection property to object for autocomplete.
for db in self._conn.database_names():
setattr(self._conn, db, Database(self._conn, db))
_db = Database(self._conn, db)
for collection in _db.collection_names():
# [TODO] change eval to other!!
setattr(eval('self._conn.'+db), collection,
Collection(_db, collection))
return self._conn
示例12: load_feature_vectors_and_classes
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def load_feature_vectors_and_classes(db_name):
"""
:param db_name: name of database to use
:return:
- list of feature vectors
- dictionary where the keys are the class labels and the values are dictionaries of the form
{start: <integer>, end: <integer>}
"""
print 'Loading feature vectors and target classes...'
db = Database(MongoClient(), db_name)
collection_names = db.collection_names()
if not ('naive_bayes' in collection_names and 'feature_vectors' in collection_names):
print 'Database missing collections needed to train classifier on.'
exit()
target_classes = Collection(db, 'naive_bayes').find_one({'type': 'classes'})
if 'classes' not in target_classes:
raise KeyError("'target_classes' must contain a 'classes' key.")
feature_vectors = list(Collection(db, 'feature_vectors').find())
return feature_vectors, target_classes['classes']
示例13: __init__
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
class DataDB:
def __init__(self, name):
db_server_ip = conf("config.db_server_ip")
db_server_port = conf("config.db_server_port")
self.__db_name = "camp{0}".format(name)
self.__connection = Connection(host=db_server_ip, port=db_server_port)
self.__db = Database(connection=self.__connection, name=self.__db_name)
# Return collection with the specified name and filter.
def get_collection(self, collection, where={}):
return self.__db[collection].find(spec=where)
# Returns the first document that matches the where clause within the specified collection.
def get_document(self, collection, where={}):
return self.__db[collection].find_one(spec_or_id=where)
# Removes the specified document.
def dispose_document(self, collection, where):
return self.__db[collection].remove(spec_or_id=where)
# Updates the document with the specified id, the document is inserted if the _id is not found.
# Returns the updated document.
def set_document(self, collection, _id, values):
if _id is None:
# If no _id to update, insert it.
_id = self.__db[collection].insert(values)
else:
# If _id to update, update it.
self.__db[collection].update(
spec={"_id": _id},
document={"$set": values},
upsert=True,
new=True)
# Return modified document.
return self.__db[collection].find_one({"_id": _id})
# Removes the specified collection from the database.
def dispose_collection(self, collection):
self.__db[collection].drop()
# Insert a bulk of documents into a collection.
def bulk_import(self, collection, doc_or_docs):
self.__db[collection].insert(doc_or_docs)
# Return the count of documents for a specific collection.
def count_documents(self, collection, where={}):
cur = self.__db[collection].find(where)
return cur.count()
def exists_collection(self, name):
return name in self.__db.collection_names()
# Moves unique records from source collection to destination, order parameter still pending.
def perform_cleanup(self, source, destination):
func = (
"function(doc){" +
("if (db.whitelist.find(doc._id).count() == 1)" if self.exists_collection("whitelists") else "") +
("if (db.blacklist.find(doc._id).count() == 0)" if self.exists_collection("blacklists") else "") +
" db.data.insert(doc._id)" +
"}")
logging.debug("Function foreach: " + func)
self.__db[destination].drop()
self.__db[source].find().forEach(bson.Code(func))
logging.debug("forEach execution completed")
示例14: show_cols
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
def show_cols( self, db ):
db = Database( self, db )
return db.collection_names()
示例15: Store
# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import collection_names [as 别名]
#.........这里部分代码省略.........
if action == 'remove':
collection.remove(mongo_doc)
obj_info.delete("store")
else:
collection.save(mongo_doc)
obj = obj_info.obj
obj_id = mongo_doc['_id']
obj._id = obj_id
self._cache[(cls_info, obj_id)] = obj
func = getattr(obj, '__thunder_flushed__', None)
if func:
func()
def get(self, cls, obj_id):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
obj = self._cache.get((cls_info, obj_id))
if obj is not None:
return obj
cursor = self._load(cls_info, collection.find,
{'_id': obj_id}, limit=2)
if cursor.count():
return self._build_doc(cls_info, cursor[0])
def find(self, cls, *args, **kwargs):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
cursor = self._load(cls_info, collection.find, *args, **kwargs)
for item in cursor:
yield self._build_doc(cls_info, item)
def find_one(self, cls, *args, **kwargs):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
item = self._load(cls_info, collection.find_one, *args, **kwargs)
if item is not None:
return self._build_doc(cls_info, item)
def find_by(self, cls, **kwargs):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
cursor = self._load(cls_info, collection.find, kwargs)
for item in cursor:
yield self._build_doc(cls_info, item)
def find_one_by(self, cls, **kwargs):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
item = self._load(cls_info, collection.find_one, **kwargs)
if item is not None:
return self._build_doc(cls_info, item)
def count(self, cls):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
return collection.find().count()
def add(self, obj):
obj_info = get_obj_info(obj)
if obj_info.get('store') is not None: # pragma: nocoverage
raise TypeError(
"Document %s is already in a store" % (obj, ))
obj_info.set('store', self)
obj_info.flush_pending = True
self.obj_infos.add(obj_info)
def remove(self, obj):
obj_info = get_obj_info(obj)
store = obj_info.get('store')
if store != self:
raise Exception("This object does not belong to this store")
obj_info.set('action', 'remove')
obj_info.flush_pending = True
self.obj_infos.add(obj_info)
del self._cache[(obj_info.cls_info, obj._id)]
def flush(self):
for obj_info in self.obj_infos:
# FIXME: Use obj_info.flush_pending
self._flush_one(obj_info)
def drop_cache(self):
self._cache = {}
def drop_collection(self, cls):
cls_info = get_cls_info(cls)
collection = cls_info.get_collection(self)
collection.drop()
def drop_collections(self):
for name in self.database.collection_names():
if name.startswith('system'):
continue
collection = self.database[name]
collection.drop()