本文整理汇总了Python中pymongo.database.Database类的典型用法代码示例。如果您正苦于以下问题:Python Database类的具体用法?Python Database怎么用?Python Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_collection
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: store_feeds
def store_feeds(feeds, collection):
'store the feeds in mongodb '
from pymongo.errors import CollectionInvalid
from pymongo.collection import Collection
from pymongo.connection import Connection
con = Connection(DB)
from pymongo.database import Database
db = Database(con, 'articles')
col = None
try:
col = db.create_collection(collection)
except CollectionInvalid as e:
col = Collection(db, collection)
for feed in feeds:
if 'title' in feed:
cursor = col.find({'title':'%s' % feed['title']})
if cursor.count() > 0:
continue
elif 'source' in feed:
cursor = col.find({'source':'%s' % feed['source']})
if cursor.count() > 0:
continue
col.save(feed)
示例3: binary_contents_test
def binary_contents_test(self):
db = Database(self._get_connection(), "pymongo_test")
test = db.create_collection("test_binary")
import os
import bson
obj = os.urandom(1024)
test.save({"hello": bson.Binary(obj)})
db.drop_collection("test_binary")
示例4: test_collection_names
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)
示例5: __init__
def __init__(self, config):
database = config['mongo_db_name']
if 'mongo_login' in config:
passwordfile = config['mongo_login']
else:
passwordfile = None
connection = MongoDB.connect(config)
Database.__init__(self, connection, database)
if passwordfile is not None and exists(passwordfile):
password = yaml.load(open(passwordfile, 'rU'))
self.authenticate(password['mongo_user'], password['mongo_password'])
示例6: test_collection_names
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."))
示例7: get_db
def get_db(is_local_deployed=False):
if is_local_deployed:
#for local launch
connection = Connection()
db = connection.wiki
return db
else:
# for dotcloud launch
mongodb_info = json.load(file("/home/dotcloud/environment.json"))
print mongodb_info
connection = Connection(mongodb_info["DOTCLOUD_DATA_MONGODB_HOST"], int(mongodb_info["DOTCLOUD_DATA_MONGODB_PORT"]))
database = Database(connection, "wiki")
database.authenticate("xinz", "hellopy")
db = connection.wiki
return db
示例8: MongoopTrigger
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
示例9: __init__
def __init__(self):
db_server_ip = conf("config.db_server_ip")
db_server_port = conf("config.db_server_port")
self.__connection = Connection(host=db_server_ip, port=db_server_port)
db_name = conf("config.db_name")
self.__db = Database(connection= self.__connection, name=db_name)
示例10: test4
def test4(self):
db = Database(self._get_connection(), "pymongo_test")
test = db.create_collection("test_4")
try:
for i in range(5):
name = "test %d" % (i)
test.save({ "user_id": i, "name": name, "group_id" : i % 10, "posts": i % 20})
test.create_index("user_id")
for i in xrange(6):
for r in test.find( { "group_id": random.randint(0,10) } ):
print "Found: %s " % (r)
finally:
db.drop_collection("test_4")
示例11: borrarindicesevitardups
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()
示例12: connect
def connect(conf, prod):
log.info('setting up mongo connection')
url = conf.get('mongo', 'url')
port = conf.getint('mongo', 'port')
db = conf.get('mongo', 'db')
conn = Connection(url, port)
db = Database(conn, db)
if (prod):
log.info('authenticating mongo connection')
username = conf.get('mongo', 'username')
pwd = conf.get('mongo', 'password')
db.authenticate(username, pwd)
return db
示例13: __init__
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)
示例14: __init__
def __init__(self, conn_string, database, trace=False):
if not conn_string.startswith('mongodb://'):
conn_string = 'mongodb://' + conn_string
self.connection = Connection(conn_string)
self.database = Database(self.connection, database)
self.trace = trace
self.collections = []
self.obj_infos = set()
self._cache = {}
示例15: store_feeds
def store_feeds(feeds, collection):
'store the feeds in mongodb '
from pymongo.errors import CollectionInvalid
from pymongo.collection import Collection
from pymongo.connection import Connection
con = Connection(DB)
from pymongo.database import Database
db = Database(con, 'queries')
col = None
try:
col = db.create_collection(collection)
except CollectionInvalid as e:
col = Collection(db, collection)
for feed in feeds:
existed = col.find_one({'query':feed}, {'$exists':'true'})
if not existed:
col.save({'query':feed})