Mongodb空间查询之C++driver

来源:互联网 发布:产品线数据分析 编辑:程序博客网 时间:2024/06/11 09:43

之前用mongodb做的地名数据库,用C++查询一直有问题。经过一番折腾,发现是查询条件的写法有问题,此前在网上也一直没有找到正确的参考资料。

方法1:返回指定范围内部的地名点

BSONArray polygon = BSON_ARRAY(BSON_ARRAY(longitude_min<<latitude_min)<<BSON_ARRAY(longitude_min<<latitude_max)<<BSON_ARRAY(longitude_max<<latitude_max)<<BSON_ARRAY(longitude_max<<latitude_min)); 
string local = polygon.toString();
auto_ptr<DBClientCursor> cursor = c.query("geolabel.placename", BSON("Coordinate"<< BSON("$within" <<BSON("$polygon" << polygon))));

方法2:返回制定点附近的地名点,并按距离排序

BSONArray point = BSON_ARRAY(lon<<lat);
string local = point.toString();
auto_ptr<DBClientCursor> cursor = c.query("geolabel.placename2", BSON("Coordinate"<< BSON("$near"<< point)));

完成这些功能,主要是靠Mongodb的自带的geohash索引。因此,这些功能需要创建“2d”索引。

属性查询中,需要用到模糊查询,涉及到regex,下次再拷贝。