本文整理汇总了Python中pymongo.database.Database类的典型用法代码示例。如果您正苦于以下问题:Python Database类的具体用法?Python Database怎么用?Python Database使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Database类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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")
开发者ID:Tokutek,项目名称:mongo-python-driver,代码行数:29,代码来源:test_database.py
示例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)
开发者ID:chengdujin,项目名称:socrates,代码行数:25,代码来源:feed_scraper.py
示例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")
开发者ID:appneta,项目名称:python-traceview,代码行数:8,代码来源:test_pymongo.py
示例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)
开发者ID:pombredanne,项目名称:mongo-python-driver,代码行数:10,代码来源:test_database.py
示例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'])
开发者ID:moma,项目名称:easiparse,代码行数:11,代码来源:mongodbhandler.py
示例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."))
开发者ID:ArturFis,项目名称:mongo-python-driver,代码行数:14,代码来源:test_database.py
示例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
开发者ID:xinz,项目名称:wikiapp,代码行数:15,代码来源:wikidb.py
示例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
开发者ID:Lujeni,项目名称:mongoop,代码行数:25,代码来源:mongodb.py
示例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)
开发者ID:manuelCordon,项目名称:broadcaster,代码行数:7,代码来源:DataAccess.py
示例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")
开发者ID:appneta,项目名称:python-traceview,代码行数:16,代码来源:test_pymongo.py
示例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()
开发者ID:carlos-gs,项目名称:MiStuRe,代码行数:17,代码来源:mongops.py
示例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
开发者ID:ariejdl,项目名称:squash-ladder,代码行数:18,代码来源:mongo_conn.py
示例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)
开发者ID:Lujeni,项目名称:mongoop,代码行数:9,代码来源:mongodb.py
示例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 = {}
开发者ID:jdahlin,项目名称:thunder,代码行数:9,代码来源:store.py
示例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})
开发者ID:chengdujin,项目名称:minerva,代码行数:19,代码来源:feeds.py
示例16: conn
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
开发者ID:Bloodevil,项目名称:ipython_mongo,代码行数:20,代码来源:imongo.py
示例17: test_get_collection
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)
开发者ID:hedgepigdaniel,项目名称:mongo-python-driver,代码行数:20,代码来源:test_database.py
示例18: test_create_collection
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")
开发者ID:jonnyhsu,项目名称:mongo-python-driver,代码行数:21,代码来源:test_database.py
示例19: init_connection
def init_connection(self):
if hasattr(self, 'app'):
config = self.app.config
else:
config = self._default_config
connection = Connection(
host=config.get('MONGODB_HOST'),
port=config.get('MONGODB_PORT'),
slave_okay=config.get('MONGODB_SLAVE_OKAY')
)
database = Database(connection, config.get('MONGODB_DATABASE'))
if config.get('MONGODB_USERNAME') is not None:
database.authenticate(
config.get('MONGODB_USERNAME'),
config.get('MONGODB_PASSWORD')
)
return connection, database
开发者ID:chopachom,项目名称:coltrane,代码行数:21,代码来源:mongodb.py
示例20: load_feature_vectors_and_classes
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']
开发者ID:S-Ercan,项目名称:NewsClassification,代码行数:21,代码来源:prepare_data.py
注:本文中的pymongo.database.Database类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论