SpringMVC下Mongo數(shù)據(jù)庫查詢距離最近的點

A、插入地理坐標(biāo)

db.mapinfo.insert({"address" : "廈門市天馬路","loc" : { "type": "Point", "coordinates": [118.768964,129.99646]}})

B、地理坐標(biāo)索引構(gòu)建

db.mapinfo.ensureIndex( { loc : "2dsphere" } )

C、查詢一定范圍內(nèi)的坐標(biāo) 倒序 單位是米

db.mapinfo.find({?"loc"?:?{?"$near"?:?{?"$geometry"?:

{?"type"?:?"Point",?"coordinates"?:?[118.783799,?31.979234]?},

"$maxDistance"?:?5000?}?}?}).limit(50);

Java存儲,存儲到mongo的地理坐標(biāo)需要重新構(gòu)建一個對象存放

導(dǎo)入架包

導(dǎo)入架包
構(gòu)建對象
保存數(shù)據(jù)

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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • =========================================================...
    lavor閱讀 3,505評論 0 5
  • show dbs:顯示數(shù)據(jù)庫列表 show collections:顯示當(dāng)前數(shù)據(jù)庫中的集合(類似關(guān)系數(shù)據(jù)庫中的表)...
    Weiliam閱讀 7,426評論 0 2
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie閱讀 2,914評論 0 3
  • 這是個陰暗的星期六,天還沒亮,有些灰蒙蒙的,天空從小雨下到豆滴大的大雨。路上的行人很少,但行人的傘同傘不停的發(fā)生碰...
    愛洛雪兒閱讀 401評論 0 1