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


Python Database.drop_collection方法代码示例

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


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

示例1: test_drop_collection

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

示例2: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例3: binary_contents_test

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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_drop_collection

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

示例5: test_drop_collection

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

示例6: test4

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例8: test_create_collection

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例9: test2

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例10: dbref_test

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例11: test1

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_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

示例12: TestCursor

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_collection [as 别名]
class TestCursor(unittest.TestCase):
    def setUp(self):
        self.db = Database(get_connection(), "pymongo_test")

    def test_explain(self):
        a = self.db.test.find()
        b = a.explain()
        for _ in a:
            break
        c = a.explain()
        del b["millis"]
        b.pop("oldPlan", None)
        del c["millis"]
        c.pop("oldPlan", None)
        self.assertEqual(b, c)
        self.assert_("cursor" in b)

    def test_hint(self):
        db = self.db
        self.assertRaises(TypeError, db.test.find().hint, 5.5)
        db.test.remove({})
        db.test.drop_indexes()

        for i in range(100):
            db.test.insert({"num": i, "foo": i})

        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("num", ASCENDING)]).explain)
        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain)

        index = db.test.create_index("num")

        spec = [("num", ASCENDING)]
        self.assertEqual(db.test.find({}).explain()["cursor"], "BasicCursor")
        self.assertEqual(db.test.find({}).hint(spec).explain()["cursor"], "BtreeCursor %s" % index)
        self.assertEqual(db.test.find({}).hint(spec).hint(None).explain()["cursor"], "BasicCursor")
        self.assertRaises(OperationFailure, db.test.find({"num": 17, "foo": 17}).hint([("foo", ASCENDING)]).explain)

        a = db.test.find({"num": 17})
        a.hint(spec)
        for _ in a:
            break
        self.assertRaises(InvalidOperation, a.hint, spec)

        self.assertRaises(TypeError, db.test.find().hint, index)

    # This is deprecated - test that a warning is actually raised
    def test_slave_okay(self):
        db = self.db
        db.drop_collection("test")
        db.test.save({"x": 1})

        warnings.simplefilter("error")

        self.assertEqual(1, db.test.find().next()["x"])
        self.assertRaises(DeprecationWarning, db.test.find, slave_okay=True)
        self.assertRaises(DeprecationWarning, db.test.find, slave_okay=False)

        warnings.simplefilter("default")

    def test_limit(self):
        db = self.db

        self.assertRaises(TypeError, db.test.find().limit, None)
        self.assertRaises(TypeError, db.test.find().limit, "hello")
        self.assertRaises(TypeError, db.test.find().limit, 5.5)

        db.test.remove({})
        for i in range(100):
            db.test.save({"x": i})

        count = 0
        for _ in db.test.find():
            count += 1
        self.assertEqual(count, 100)

        count = 0
        for _ in db.test.find().limit(20):
            count += 1
        self.assertEqual(count, 20)

        count = 0
        for _ in db.test.find().limit(99):
            count += 1
        self.assertEqual(count, 99)

        count = 0
        for _ in db.test.find().limit(1):
            count += 1
        self.assertEqual(count, 1)

        count = 0
        for _ in db.test.find().limit(0):
            count += 1
        self.assertEqual(count, 100)

        count = 0
        for _ in db.test.find().limit(0).limit(50).limit(10):
            count += 1
        self.assertEqual(count, 10)

#.........这里部分代码省略.........
开发者ID:virtix,项目名称:mongo-python-driver,代码行数:103,代码来源:test_cursor.py

示例13: TestCursor

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_collection [as 别名]

#.........这里部分代码省略.........
        curs = db.test.find().limit(50).batch_size(500)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        curs = db.test.find().batch_size(500)
        curs.next()
        self.assertEqual(500, curs._Cursor__retrieved)

        curs = db.test.find().limit(50)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        # these two might be shaky, as the default
        # is set by the server. as of 2.0.0-rc0, 101
        # or 1MB (whichever is smaller) is default
        # for queries without ntoreturn
        curs = db.test.find()
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

        curs = db.test.find().limit(0).batch_size(0)
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

    def test_skip(self):
        db = self.db

        self.assertRaises(TypeError, db.test.find().skip, None)
        self.assertRaises(TypeError, db.test.find().skip, "hello")
        self.assertRaises(TypeError, db.test.find().skip, 5.5)
        self.assertRaises(ValueError, db.test.find().skip, -5)
        self.assertTrue(db.test.find().skip(5L))

        db.drop_collection("test")

        for i in range(100):
            db.test.save({"x": i})

        for i in db.test.find():
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(20):
            self.assertEqual(i["x"], 20)
            break

        for i in db.test.find().skip(99):
            self.assertEqual(i["x"], 99)
            break

        for i in db.test.find().skip(1):
            self.assertEqual(i["x"], 1)
            break

        for i in db.test.find().skip(0):
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(0).skip(50).skip(10):
            self.assertEqual(i["x"], 10)
            break

        for i in db.test.find().skip(1000):
            self.fail()

        a = db.test.find()
开发者ID:AlexSnet,项目名称:oneline,代码行数:70,代码来源:test_cursor.py

示例14: TestCursor

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_collection [as 别名]

#.........这里部分代码省略.........
        self.assertEqual(4, curs._Cursor__retrieved)

        curs = db.test.find().limit(50).batch_size(500)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        curs = db.test.find().batch_size(500)
        curs.next()
        self.assertEqual(500, curs._Cursor__retrieved)

        curs = db.test.find().limit(50)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        # these two might be shaky, as the default
        # is set by the server. as of 2.0.0-rc0, 101
        # or 1MB (whichever is smaller) is default
        # for queries without ntoreturn
        curs = db.test.find()
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

        curs = db.test.find().limit(0).batch_size(0)
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

    def test_skip(self):
        db = self.db

        self.assertRaises(TypeError, db.test.find().skip, None)
        self.assertRaises(TypeError, db.test.find().skip, "hello")
        self.assertRaises(TypeError, db.test.find().skip, 5.5)

        db.drop_collection("test")

        for i in range(100):
            db.test.save({"x": i})

        for i in db.test.find():
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(20):
            self.assertEqual(i["x"], 20)
            break

        for i in db.test.find().skip(99):
            self.assertEqual(i["x"], 99)
            break

        for i in db.test.find().skip(1):
            self.assertEqual(i["x"], 1)
            break

        for i in db.test.find().skip(0):
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(0).skip(50).skip(10):
            self.assertEqual(i["x"], 10)
            break

        for i in db.test.find().skip(1000):
            self.fail()

        a = db.test.find()
开发者ID:bewie,项目名称:mongo-python-driver,代码行数:70,代码来源:test_cursor.py

示例15: TestCursor

# 需要导入模块: from pymongo.database import Database [as 别名]
# 或者: from pymongo.database.Database import drop_collection [as 别名]

#.........这里部分代码省略.........
        curs = db.test.find().limit(50).batch_size(500)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        curs = db.test.find().batch_size(500)
        curs.next()
        self.assertEqual(500, curs._Cursor__retrieved)

        curs = db.test.find().limit(50)
        curs.next()
        self.assertEqual(50, curs._Cursor__retrieved)

        # these two might be shaky, as the default
        # is set by the server. as of 2.0.0-rc0, 101
        # or 1MB (whichever is smaller) is default
        # for queries without ntoreturn
        curs = db.test.find()
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

        curs = db.test.find().limit(0).batch_size(0)
        curs.next()
        self.assertEqual(101, curs._Cursor__retrieved)

    def test_skip(self):
        db = self.db

        self.assertRaises(TypeError, db.test.find().skip, None)
        self.assertRaises(TypeError, db.test.find().skip, "hello")
        self.assertRaises(TypeError, db.test.find().skip, 5.5)
        self.assertRaises(ValueError, db.test.find().skip, -5)
        self.assertTrue(db.test.find().skip(5L))

        db.drop_collection("test")

        for i in range(100):
            db.test.save({"x": i})

        for i in db.test.find():
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(20):
            self.assertEqual(i["x"], 20)
            break

        for i in db.test.find().skip(99):
            self.assertEqual(i["x"], 99)
            break

        for i in db.test.find().skip(1):
            self.assertEqual(i["x"], 1)
            break

        for i in db.test.find().skip(0):
            self.assertEqual(i["x"], 0)
            break

        for i in db.test.find().skip(0).skip(50).skip(10):
            self.assertEqual(i["x"], 10)
            break

        for i in db.test.find().skip(1000):
            self.fail()

        a = db.test.find()
开发者ID:goinnn,项目名称:mongo-python-driver,代码行数:70,代码来源:test_cursor.py


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