当前位置: 首页>>代码示例>>Python>>正文


Python Database.create_collection方法代码示例

本文整理汇总了Python中pymongo.database.Database.create_collection方法的典型用法代码示例。如果您正苦于以下问题:Python Database.create_collection方法的具体用法?Python Database.create_collection怎么用?Python Database.create_collection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pymongo.database.Database的用法示例。


在下文中一共展示了Database.create_collection方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [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")
开发者ID:Tokutek,项目名称:mongo-python-driver,代码行数:31,代码来源:test_database.py

示例2: store_feeds

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
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,代码行数:27,代码来源:feed_scraper.py

示例3: binary_contents_test

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
 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,代码行数:10,代码来源:test_pymongo.py

示例4: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [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")
开发者ID:jonnyhsu,项目名称:mongo-python-driver,代码行数:23,代码来源:test_database.py

示例5: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [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")
开发者ID:pombredanne,项目名称:mongo-python-driver,代码行数:24,代码来源:test_database.py

示例6: test4

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
    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,代码行数:18,代码来源:test_pymongo.py

示例7: store_feeds

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
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,代码行数:21,代码来源:feeds.py

示例8: test2

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
    def test2(self):
        db = Database(self._get_connection(), "pymongo_test")
        test = db.create_collection("test_2")
        try:
            for i in range(100):
                name = "test %d" % (i)
                ret = test.save({"name": name, "group_id" : i % 3, "posts": i % 20})
                print "Save Ret: %s" % (ret)

            ret = test.update({"posts": 10}, {"$set": {"posts": 100}}, multi=True, safe=True)
            #ret = test.update({"posts": 10}, {"$set": {"posts": 100}}, multi=True)
            print "Update Ret: %s" % (ret)
            test.update({"name": "test 2"}, {"$set": {"posts": 200}})
            test.create_index("posts")
            test.ensure_index("posts")

            for r in test.find({"posts":100}):
                print "Found: %s" % (r,)

            ret = test.remove({"posts": 1}, safe=True)
            print "Remove Ret: %s" % (ret)

            groups = test.group(
                key={"group_id":1},
                condition=None,
                initial={"post_sum":0},
                reduce="function(obj,prev) {prev.post_sum++;}"
            )
            for g in groups:
                print "Group: %s" % (g,)

            for d in test.distinct('posts'):
                print "Distinct: %s" % (d,)

            if 'reindex' in dir(test):
                test.reindex()
            test.drop_indexes()
        finally:
            db.drop_collection("test_2")
开发者ID:appneta,项目名称:python-traceview,代码行数:41,代码来源:test_pymongo.py

示例9: dbref_test

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
    def dbref_test(self):
        db = Database(self._get_connection(), "pymongo_test")

        try:
            db.create_collection('owners')
            db.create_collection('tasks')
            db.create_collection('tasks_ref')

            # owners and tasks
            db.owners.insert({"name":"Jim"})
            db.tasks.insert([
                {"name": "read"},
                {"name": "sleep"}
                ])

            # update jim with tasks: reading and sleeping
            reading_task = db.tasks.find_one({"name": "read"})
            sleeping_task = db.tasks.find_one({"name": "sleep"})

            jim_update = db.owners.find_one({"name": "Jim"})
            jim_update["tasks"] = [
                DBRef(collection = "tasks", id = reading_task["_id"]),
                DBRef(collection = "tasks", id = sleeping_task["_id"])
                ]

            db.owners.save(jim_update)

            # get jim fresh again and display his tasks
            fresh_jim = db.owners.find_one({"name":"Jim"})
            print "tasks are:"
            for task in fresh_jim["tasks"]:
                print db.dereference(task)["name"]

            db.tasks_ref.insert( { "ref" :  DBRef(collection = "tasks", id = reading_task["_id"]) })
            db.tasks_ref.insert( { "ref" :  DBRef(collection = "tasks", id = sleeping_task["_id"]) })
            r1 = db.tasks_ref.find( { "ref" : DBRef(collection = "tasks", id = reading_task["_id"]) })
            print r1.count()
        finally:
            db.drop_collection('owners')
            db.drop_collection('tasks')
            db.drop_collection('tasks_ref')
开发者ID:appneta,项目名称:python-traceview,代码行数:43,代码来源:test_pymongo.py

示例10: test_list_collections

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
    def test_list_collections(self):
        self.client.drop_database("pymongo_test")
        db = Database(self.client, "pymongo_test")
        db.test.insert_one({"dummy": u"object"})
        db.test.mike.insert_one({"dummy": u"object"})

        results = db.list_collections()
        colls = [result["name"] for result in results]

        # All the collections present.
        self.assertTrue("test" in colls)
        self.assertTrue("test.mike" in colls)

        # No collection containing a '$'.
        for coll in colls:
            self.assertTrue("$" not in coll)

        # Duplicate check.
        coll_cnt = {}
        for coll in colls:
            try:
                # Found duplicate.
                coll_cnt[coll] += 1
                self.assertTrue(False)
            except KeyError:
                coll_cnt[coll] = 1
        coll_cnt = {}

        # Checking if is there any collection which don't exists.
        if (len(set(colls) - set(["test","test.mike"])) == 0 or
            len(set(colls) - set(["test","test.mike","system.indexes"])) == 0):
            self.assertTrue(True)
        else:
            self.assertTrue(False)

        colls = db.list_collections(filter={"name": {"$regex": "^test$"}})
        self.assertEqual(1, len(list(colls)))

        colls = db.list_collections(filter={"name": {"$regex": "^test.mike$"}})
        self.assertEqual(1, len(list(colls)))

        db.drop_collection("test")

        db.create_collection("test", capped=True, size=4096)
        results = db.list_collections(filter={'options.capped': True})
        colls = [result["name"] for result in results]

        # Checking only capped collections are present
        self.assertTrue("test" in colls)
        self.assertFalse("test.mike" in colls)

        # No collection containing a '$'.
        for coll in colls:
            self.assertTrue("$" not in coll)

        # Duplicate check.
        coll_cnt = {}
        for coll in colls:
            try:
                # Found duplicate.
                coll_cnt[coll] += 1
                self.assertTrue(False)
            except KeyError:
                coll_cnt[coll] = 1
        coll_cnt = {}

        # Checking if is there any collection which don't exists.
        if (len(set(colls) - set(["test"])) == 0 or
            len(set(colls) - set(["test","system.indexes"])) == 0):
            self.assertTrue(True)
        else:
            self.assertTrue(False)

        self.client.drop_database("pymongo_test")
开发者ID:ShaneHarvey,项目名称:mongo-python-driver,代码行数:76,代码来源:test_database.py

示例11: test1

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import create_collection [as 别名]
 def test1(self):
     db = Database(self._get_connection(), "pymongo_test")
     test = db.create_collection("test_1_4")
     test.save({"hello": u"world"})
     test.rename("test_1_new")
     db.drop_collection("test_1_new")
开发者ID:appneta,项目名称:python-traceview,代码行数:8,代码来源:test_pymongo.py


注:本文中的pymongo.database.Database.create_collection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。