コード例 #1
0
    def test_index_info(self):
        db = self.db

        yield from db.test.drop_indexes()
        yield from db.test.remove({})

        yield from db.test.save({})  # create collection
        ix_info = yield from db.test.index_information()
        self.assertEqual(len(ix_info), 1)

        self.assert_("_id_" in ix_info)

        yield from db.test.create_index(filter.sort(filter.ASCENDING("hello")))
        ix_info = yield from db.test.index_information()
        self.assertEqual(len(ix_info), 2)
        
        self.assertEqual(ix_info["hello_1"], [("hello", 1)])

        yield from db.test.create_index(filter.sort(filter.DESCENDING("hello") + filter.ASCENDING("world")), unique=True)
        ix_info = yield from db.test.index_information()

        self.assertEqual(ix_info["hello_1"], [("hello", 1)])
        self.assertEqual(len(ix_info), 3)
        self.assertEqual([("world", 1), ("hello", -1)], ix_info["hello_-1_world_1"])
        # Unique key will not show until index_information is updated with changes introduced in version 1.7
        #self.assertEqual(True, ix_info["hello_-1_world_1"]["unique"])

        yield from db.test.drop_indexes()
        yield from db.test.remove({})
コード例 #2
0
ファイル: index.py プロジェクト: jettify/asyncio_mongo
def example():
    mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(filter.ASCENDING("something") + filter.DESCENDING("else"))
    print("IDX:", idx)

    result = yield from test.create_index(idx)
    print("create_index:", result)

    result = yield from test.index_information()
    print("index_information:", result)

    result = yield from test.drop_index(idx)
    print("drop_index:", result)

    # Geohaystack example
    geoh_idx = filter.sort(filter.GEOHAYSTACK("loc") + filter.ASCENDING("type"))
    print("IDX:", geoh_idx)
    result = yield from test.create_index(geoh_idx, **{'bucketSize':1})
    print("index_information:", result)
    
    result = yield from test.drop_index(geoh_idx)
    print("drop_index:", result)

    # 2D geospatial index
    geo_idx = filter.sort(filter.GEO2D("pos"))
    print("IDX:", geo_idx)
    result = yield from test.create_index(geo_idx, **{ 'min':-100, 'max':100 })
    print("index_information:", result)

    result = yield from test.drop_index(geo_idx)
    print("drop_index:", result)
コード例 #3
0
    def test_index_haystack(self):
        db = self.db
        coll = self.coll
        yield from coll.drop_indexes()

        _id = yield from coll.insert({
            "pos": {"long": 34.2, "lat": 33.3},
            "type": "restaurant"
        })
        yield from coll.insert({
            "pos": {"long": 34.2, "lat": 37.3}, "type": "restaurant"
        })
        yield from coll.insert({
            "pos": {"long": 59.1, "lat": 87.2}, "type": "office"
        })

        yield from coll.create_index(filter.sort(filter.GEOHAYSTACK("pos") + filter.ASCENDING("type")), **{'bucket_size': 1})

        # TODO: A db.command method has not been implemented yet.
        # Sending command directly
        command = SON([
            ("geoSearch", "mycol"),
            ("near", [33, 33]),
            ("maxDistance", 6),
            ("search", {"type": "restaurant"}),
            ("limit", 30),
        ])
           
        results = yield from db["$cmd"].find_one(command)
        self.assertEqual(2, len(results['results']))
        self.assertEqual({
            "_id": _id,
            "pos": {"long": 34.2, "lat": 33.3},
            "type": "restaurant"
        }, results["results"][0])
コード例 #4
0
    def test_Timestamps(self):
        """Tests mongo operations with Timestamps"""
        test = self.coll

        # insert with specific timestamp
        doc1 = {'_id': objectid.ObjectId(), 'ts': timestamp.Timestamp(1, 2)}
        yield from test.insert(doc1, safe=True)

        result = yield from test.find_one(doc1)
        self.assertEqual(result.get('ts').time, 1)
        self.assertEqual(result.get('ts').inc, 2)

        # insert with specific timestamp
        doc2 = {'_id': objectid.ObjectId(), 'ts': timestamp.Timestamp(2, 1)}
        yield from test.insert(doc2, safe=True)

        # the objects come back sorted by ts correctly.
        # (test that we stored inc/time in the right fields)
        result = yield from test.find(filter=qf.sort(qf.ASCENDING('ts')))
        self.assertEqual(result[0]['_id'], doc1['_id'])
        self.assertEqual(result[1]['_id'], doc2['_id'])

        # insert with null timestamp
        doc3 = {'_id': objectid.ObjectId(), 'ts': timestamp.Timestamp(0, 0)}
        yield from test.insert(doc3, safe=True)

        # time field loaded correctly
        result = yield from test.find_one(doc3['_id'])
        now = time.time()
        self.assertTrue(now - 2 <= result['ts'].time <= now)

        # delete
        yield from test.remove(doc1["_id"], safe=True)
        yield from test.remove(doc2["_id"], safe=True)
        yield from test.remove(doc3["_id"], safe=True)
コード例 #5
0
    def test_create_index_nodup(self):
        coll = self.coll

        yield from coll.drop()
        yield from coll.insert({'b': 1})
        yield from coll.insert({'b': 1})

        self.assertRaises(errors.DuplicateKeyError, coll.create_index, filter.sort(filter.ASCENDING("b")), unique=True)
コード例 #6
0
 def translate_sorting(self, sorting):
     try:
         return qf.sort({
             sort.Ascending: qf.DESCENDING,
             sort.Descending: qf.ASCENDING,
         }[sorting.__class__](sorting.field._name))
     except KeyError:
         raise exceptions.UnsupportedOperation(sorting)
コード例 #7
0
    def test_ensure_index(self):
        db = self.db
        coll = self.coll
        
        yield from coll.ensure_index(filter.sort(filter.ASCENDING("hello")))
        indices = yield from db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield from coll.drop_indexes()
コード例 #8
0
    def test_index_geo2d(self):
        db = self.db
        coll = self.coll 
        yield from coll.drop_indexes()
        geo_ix = yield from coll.create_index(filter.sort(filter.GEO2D("loc")))

        self.assertEqual('loc_2d', geo_ix)

        index_info = yield from coll.index_information()
        self.assertEqual([('loc', '2d')], index_info['loc_2d'])
コード例 #9
0
    def test_create_index(self):
        db = self.db
        coll = self.coll

        self.assertRaises(TypeError, coll.create_index, 5)
        self.assertRaises(TypeError, coll.create_index, {"hello": 1})

        yield from coll.drop_indexes()
        count = yield from db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        result1 = yield from coll.create_index(filter.sort(filter.ASCENDING("hello")))
        result2 = yield from coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                          filter.DESCENDING("world")))

        count = yield from db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 3)

        yield from coll.drop_indexes()
        ix = yield from coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")), name="hello_world")
        self.assertEquals(ix, "hello_world")

        yield from coll.drop_indexes()
        count = yield from db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)
        
        yield from coll.create_index(filter.sort(filter.ASCENDING("hello")))
        indices = yield from db.system.indexes.find({"ns": u"mydb.mycol"})
        self.assert_(u"hello_1" in [a["name"] for a in indices])

        yield from coll.drop_indexes()
        count = yield from db.system.indexes.count({"ns": u"mydb.mycol"})
        self.assertEqual(count, 1)

        ix = yield from coll.create_index(filter.sort(filter.ASCENDING("hello") + \
                                   filter.DESCENDING("world")))
        self.assertEquals(ix, "hello_1_world_-1")
コード例 #10
0
def example():
    mongo = yield from asyncio_mongo.Connection.create('localhost', 27017)

    foo = mongo.foo  # `foo` database
    test = foo.test  # `test` collection

    idx = filter.sort(
        filter.ASCENDING("something") + filter.DESCENDING("else"))
    print("IDX:", idx)

    result = yield from test.create_index(idx)
    print("create_index:", result)

    result = yield from test.index_information()
    print("index_information:", result)

    result = yield from test.drop_index(idx)
    print("drop_index:", result)

    # Geohaystack example
    geoh_idx = filter.sort(
        filter.GEOHAYSTACK("loc") + filter.ASCENDING("type"))
    print("IDX:", geoh_idx)
    result = yield from test.create_index(geoh_idx, **{'bucketSize': 1})
    print("index_information:", result)

    result = yield from test.drop_index(geoh_idx)
    print("drop_index:", result)

    # 2D geospatial index
    geo_idx = filter.sort(filter.GEO2D("pos"))
    print("IDX:", geo_idx)
    result = yield from test.create_index(geo_idx, **{'min': -100, 'max': 100})
    print("index_information:", result)

    result = yield from test.drop_index(geo_idx)
    print("drop_index:", result)
コード例 #11
0
ファイル: test_objects.py プロジェクト: jettify/asyncio_mongo
    def test_Timestamps(self):
        """Tests mongo operations with Timestamps"""
        test = self.coll

        # insert with specific timestamp
        doc1 = {'_id': objectid.ObjectId(),
                'ts': timestamp.Timestamp(1, 2)}
        yield from test.insert(doc1, safe=True)

        result = yield from test.find_one(doc1)
        self.assertEqual(result.get('ts').time, 1)
        self.assertEqual(result.get('ts').inc, 2)

        # insert with specific timestamp
        doc2 = {'_id': objectid.ObjectId(),
                'ts': timestamp.Timestamp(2, 1)}
        yield from test.insert(doc2, safe=True)

        # the objects come back sorted by ts correctly.
        # (test that we stored inc/time in the right fields)
        result = yield from test.find(filter=qf.sort(qf.ASCENDING('ts')))
        self.assertEqual(result[0]['_id'], doc1['_id'])
        self.assertEqual(result[1]['_id'], doc2['_id'])

        # insert with null timestamp
        doc3 = {'_id': objectid.ObjectId(),
                'ts': timestamp.Timestamp(0, 0)}
        yield from test.insert(doc3, safe=True)

        # time field loaded correctly
        result = yield from test.find_one(doc3['_id'])
        now = time.time()
        self.assertTrue(now - 2 <= result['ts'].time <= now)

        # delete
        yield from test.remove(doc1["_id"], safe=True)
        yield from test.remove(doc2["_id"], safe=True)
        yield from test.remove(doc3["_id"], safe=True)
コード例 #12
0
 async def ensure_index(self, name, indices):
     for keys, opts in indices:
         await self.connection[name].ensure_index(
             qf.sort([(key, opts.get('order', 1)) for key in keys]),
             unique=opts.get('unique', False)
         )