def example(): mongo = yield txmongo.MongoConnection() foo = mongo.foo # `foo` database test = foo.test # `test` collection idx = filter.sort(filter.ASCENDING("something") + filter.DESCENDING("else")) print "IDX:", idx result = yield test.create_index(idx) print "create_index:", result result = yield test.index_information() print "index_information:", result result = yield 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 test.create_index(geoh_idx, **{'bucketSize':1}) print "index_information:", result result = yield 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 test.create_index(geo_idx, **{ 'min':-100, 'max':100 }) print "index_information:", result result = yield test.drop_index(geo_idx) print "drop_index:", result
def test_index_haystack(self): db = self.db coll = self.coll yield coll.drop_indexes() _id = yield coll.insert({ "pos": { "long": 34.2, "lat": 33.3 }, "type": "restaurant" }) yield coll.insert({ "pos": { "long": 34.2, "lat": 37.3 }, "type": "restaurant" }) yield coll.insert({ "pos": { "long": 59.1, "lat": 87.2 }, "type": "office" }) yield 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 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])
def test_index_haystack(self): db = self.db coll = self.coll yield coll.drop_indexes() _id = yield coll.insert({ "pos": { "long": 34.2, "lat": 33.3 }, "type": "restaurant" }) yield coll.insert({ "pos": { "long": 34.2, "lat": 37.3 }, "type": "restaurant" }) yield coll.insert({ "pos": { "long": 59.1, "lat": 87.2 }, "type": "office" }) yield coll.create_index( qf.sort(qf.GEOHAYSTACK("pos") + qf.ASCENDING("type")), **{"bucket_size": 1}) results = yield db.command("geoSearch", "mycol", near=[33, 33], maxDistance=6, search={"type": "restaurant"}, limit=30) self.assertEqual(2, len(results["results"])) self.assertEqual( { "_id": _id, "pos": { "long": 34.2, "lat": 33.3 }, "type": "restaurant" }, results["results"][0])
def test_SortGeoIndexes(self): self.assertEqual(qf.sort(qf.GEO2D('x')), qf.sort([('x', "2d")])) self.assertEqual(qf.sort(qf.GEO2DSPHERE('x')), qf.sort([('x', "2dsphere")])) self.assertEqual(qf.sort(qf.GEOHAYSTACK('x')), qf.sort([('x', "geoHaystack")]))