A、插入地理坐标
db.mapinfo.insert({"address" : "厦门市天马路","loc" : { "type": "Point", "coordinates": [118.768964,129.99646]}})
B、地理坐标索引构建
db.mapinfo.ensureIndex( { loc : "2dsphere" } )
C、查询一定范围内的坐标 倒序 单位是米
db.mapinfo.find({ "loc" : { "$near" : { "$geometry" :
{ "type" : "Point", "coordinates" : [118.783799, 31.979234] },
"$maxDistance" : 5000 } } }).limit(50);
Java存储,存储到mongo的地理坐标需要重新构建一个对象存放
导入架包
Java查询
DBObjectquery=newBasicDBObject();
// DBObjectfildes= new BasicDBObject();
// fildes.put("id", true);
query.put(
"geomp",
newBasicDBObject("$nearSphere",newBasicDBObject("$geometry",
newBasicDBObject("type","Point").append(
"coordinates",newdouble[] {lng,lat}))
.append("$maxDistance",meter)));
if(StringFormatter.strIsNotNull(businessMongoEntity.getName())) {
Patternpattern= Pattern.compile(
"^.*"+businessMongoEntity.getName() +".*$",
Pattern.CASE_INSENSITIVE);
query.put("name",pattern);
}
Queryquery1=newBasicQuery(query);
query1.skip(fromcount);
query1.limit(page.getSize());
Listbme=template.find(query1,
BusinessMongoEntity.class);
inttotal=template.getCollection("businessMongoEntity").find(query).count();
// List list =
// template.getCollection("businessMongoEntity").find(query).skip(10).limit(20).toArray();
page.setRecords(bme);
page.setTotal(total);
returnpage;
Java更新
DBObjectdbObject=newBasicDBObject();
dbObject.put("registernum",businessMongoEntity.getRegisternum());
DBObjectdbObjectupdate=newBasicDBObject();
dbObjectupdate.put("imgurl",businessMongoEntity.getImgurl());
dbObjectupdate.put("openid",businessMongoEntity.getOpenid());
dbObjectupdate.put("geomp",businessMongoEntity.getGeomp());
dbObjectupdate.put("username",businessMongoEntity.getUsername());
dbObjectupdate.put("password",businessMongoEntity.getPassword());
//template.getCollection("businessMongoEntity").update(dbObject, dbObjectupdate);
Updateupdate= Update.update("geomp",businessMongoEntity.getGeomp())
.set("imgurl",businessMongoEntity.getImgurl())
.set("openid",businessMongoEntity.getOpenid())
.set("username",businessMongoEntity.getUsername()).set("password",businessMongoEntity.getPassword());
Queryquery=newBasicQuery(dbObject);
template.upsert(query,update, BusinessMongoEntity.class);