MongoDB支持二维空间索引,使用空间索引,可以进行地理位置的检索。

前提条件:
建立空间索引的key可以使用array或内嵌文档存储,但是前两个elements必须存储固定的一对空间位置数值。如

{ loc : [ 50 , 30 ] }
{ loc : { x : 50 , y : 30 } }
{ loc : { foo : 50 , y : 30 } }
{ loc : { lat : 40.739037, long: 73.992964 } }

还需要建立索引:

db.mapinfo.ensureIndex({"loc" : "2d"},{"background" : true})

查看索引是否建立:

db.mapinfo.getIndexes()

测试查询:

db.mapinfo.find({loc : {$near : [72,82]},"category" : "coffee"})

Spring集成MongoDB:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    default-autowire="byName">
    <context:property-placeholder location="classpath*:mongodb.properties"/>
                   
    <!--mongodb-->
    <mongo:mongo host="${mongodb.host}" port="${mongodb.port}" >
        <mongo:options connections-per-host="${mongodb.connectionPperHost}"
            threads-allowed-to-block-for-connection-multiplier="${mongodb.threadsAllowedToBlockForConnectionMmultiplier}"
            connect-timeout="${mongodb.connectTimeout}" max-wait-time="${mongodb.connectTimeout}" auto-connect-retry="${mongodb.autoConnectRetry}"
            socket-keep-alive="${mongodb.socketKeepAlive}" socket-timeout="${mongodb.socketTimeout}" slave-ok="${mongodb.slaveOk}"
            write-number="${mongodb.writeNumber}" write-timeout="${mongodb.writeTimeout}" write-fsync="${mongodb.writeFsync}" />
    </mongo:mongo>
    <mongo:db-factory mongo-ref="mongo" dbname="${mongodb.dbname}" username="${mongodb.username}" password="${mongodb.password}"
        id="mongoDbFactory" write-concern="SAFE" />
    <bean id="mappingContext"
        class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
    <bean id="defaultMongoTypeMapper"
        class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
        <constructor-arg name="typeKey">
            <null />
        </constructor-arg>
    </bean>
    <bean id="mappingMongoConverter"
        class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        <constructor-arg name="mappingContext" ref="mappingContext" />
        <property name="typeMapper" ref="defaultMongoTypeMapper" />
    </bean>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
        <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
        <property name="writeResultChecking" value="EXCEPTION" />
    </bean>
</beans>


属性文件:mongodb.properties

mongodb.host=127.0.0.1
mongodb.port=27017
mongodb.dbname=mzsx
mongodb.collect=parent
mongodb.username=mzsx
mongodb.password=mzsx
mongodb.connectionPperHost=10
mongodb.threadsAllowedToBlockForConnectionMmultiplier=4
mongodb.connectTimeout=5000
mongodb.maxWaitTime=5000
mongodb.autoConnectRetry=true
mongodb.socketKeepAlive=true
mongodb.socketTimeout=500000
mongodb.slaveOk=true
mongodb.writeNumber=10
mongodb.writeTimeout=0
mongodb.writeFsync=true
mongodb.connections=22
mongodb.connectionThreads=22
mongodb.connectionsPerHost=10
mongodb.threadsMultiplier=4


java后台代码:

package com.digitalchina.lbs.mongo.impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.CollectionOptions;
import org.springframework.data.mongodb.core.DefaultIndexOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.Box;
import org.springframework.data.mongodb.core.geo.Circle;
import org.springframework.data.mongodb.core.geo.Point;
import org.springframework.data.mongodb.core.geo.Polygon;
import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.Index.Duplicates;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import com.digitalchina.lbs.mongo.api.MongoLBSAPI;
import com.digitalchina.lbs.serve.util.Distance;
import com.digitalchina.lbs.serve.util.InformationVo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import edu.emory.mathcs.backport.java.util.Collections;
@Component
public class MongoLBSImpl implements MongoLBSAPI {
    private final static Logger log=Logger.getLogger(MongoLBSImpl.class);
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     *方法:分页查找数据库中以对角线为轴的矩形内的数据
     *
     * @param lowerLeft表示左下顶点的经纬坐标,upperRight表示右上顶点的经纬坐标,classzz表示类的全称名,radius表示半径,key表示关键字
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> boxByPage(String collectionName,Query query ,double[] lowerLeft,double[] upperRight,  Class classzz, int startNum,int limit) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                log.warn("传给MongoLBSImpl类中的boxByPage()的query为null。");
                query = new Query();
            }
            // 多边形
            Box box = new Box(lowerLeft, upperRight);
            query.addCriteria(Criteria.where("infoMap.loc").within(box));
            /*if(key!=null){
                query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
            }*/
            query.skip(startNum);
            query.limit(limit);
            return mongoTemplate.find(query, classzz,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的boxByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:分页查找数据库中以对角线为轴的矩形内的数据
     *
     * @param lowerLeft表示左下顶点的经纬,upperRight表示右上顶点的经纬,classzz表示类的全称名,radius表示半径,key表示关键字
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> boxByPage(String collectionName,Query query ,Point lowerLeft, Point upperRight, Class classzz, int startNum, int limit) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                log.warn("传给MongoLBSImpl类中的boxByPage()的query为null。");
                query = new Query();
            }
            //矩形
            Box box = new Box(lowerLeft, upperRight);
            query.addCriteria(Criteria.where("infoMap.loc").within(box));
            /*if(key!=null){
                query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
            }*/
            query.skip(startNum);
            query.limit(limit);
              
            return mongoTemplate.find(query, classzz,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的boxByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
    // 方圆,分页,模糊
    /**
     *方法:分页查找数据库中以某点为圆心且指定半径内的数据
     *
     * @param centerX表示圆心的纬度,centerY表示圆心的经度,classzz表示类的全称名,radius表示半径,key表示关键字
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> circleByPage(String collectionName,Query query ,double centerX, double centerY,double radius,Class classzz, int startNum, int limit) {
        System.out.println("============MongoLBSImpl=======circleCriteria===========");
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                log.warn("传给MongoLBSImpl类中的circleByPage()的query为null。");
                query = new Query();
            }
            // 圆
            Circle cri = new Circle(centerX, centerY, Distance.getChange(radius));
            query.addCriteria(Criteria.where("infoMap.loc").within(cri));
            query.skip(startNum);
            query.limit(limit);
            //System.out.println("-=-==---=-=--=-=-=-=-========-====:"+query);
            //System.out.println(mongoTemplate.find(query, classzz,collectionName));
            List<InformationVo> list=mongoTemplate.find(query, classzz,collectionName);
            if (list!=null) {
                log.info("传给MongoLBSImpl类中的circleByPage()计算距离。");
                for (InformationVo informationVo : list) {
                    informationVo.setDistance(Distance.GetDistance(centerX, centerY, ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1)));
                }
                Collections.sort(list);
            }
              
            return list;
              
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的circleByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
    /**
     *方法:分页查找数据库中以某点为圆心且指定半径内的数据
     *
     * @param centerX表示圆心的纬度,centerY表示圆心的经度,classzz表示类的全称名,radius表示半径,key表示关键字
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> circleByPage(String collectionName,Query query ,Point center,double radius,Class classzz, int startNum, int limit){
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                log.warn("传给MongoLBSImpl类中的circleByPage()的query为null。");
                query = new Query();
            }
            // 圆
            Circle cri = new Circle(center, Distance.getChange(radius));
            query.addCriteria(Criteria.where("infoMap.loc").within(cri));
            query.skip(startNum);
            query.limit(limit);
            List<InformationVo> list=mongoTemplate.find(query, classzz,collectionName);
            if (list!=null) {
                log.info("传给MongoLBSImpl类中的circleByPage()计算距离。");
                for (InformationVo informationVo : list) {
                    informationVo.setDistance(Distance.GetDistance(center.getX(), center.getY(), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1)));
                }
                  
            }
            Collections.sort(list);
            return list;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的circleByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
          
    }
      
    // 条件查找,并分页
    /**
     *方法:根据条件分页查找数据库中指定的数据
     *
     * @param startNum表示起始数,limit表示个数,criteria表示指定的条件
     *@return List<InformationVo>
     * **/
    public List<InformationVo> conditionByPage(String collectionName,int startNum, int limit,Criteria... criteria) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的conditionByPage()中collectionName为null");
            return null;
        }
        try {
            // 分页
            Query query = new Query();
            query.skip(startNum);
            query.limit(limit);
            for (Criteria c : criteria) {
                query.addCriteria(c);
            }
            return mongoTemplate.find(query, InformationVo.class,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的conditionByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:根据条件分页查找数据库中指定的数据
     *
     * @param startNum表示起始数,limit表示个数,query表示指定的条件
     *@return List<InformationVo>
     * **/
    public List<InformationVo> conditionByPage(String collectionName,int startNum, int limit,Query query) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
            return null;
        }
        try {
            // 分页
            if (query==null) {
                query = new Query();
            }
            query.skip(startNum);
            query.limit(limit);
            /*for (Criteria c : criteria) {
                query.addCriteria(c);
            }*/
            return mongoTemplate.find(query, InformationVo.class,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的conditionByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:建立指定名称的集合
     *
     * @param collectionName表示指定的集合名称;collectionOptions表示建立集合的选项(CollectionOptions(Integer size, Integer maxDocuments, Boolean capped),size表示集合的存储空间单位为byte,maxDocuments表示最大文档数,capped表示是否为固定集合)
     *@return boolean
     * **/
    public boolean createCollection(String collectionName,
            CollectionOptions collectionOptions) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的createCollection()中collectionName为null");
            return false;
        }
        try {
            if (collectionOptions!=null) {
                log.info("执行createCollection("+collectionName+","+collectionOptions+");");
                mongoTemplate.createCollection(collectionName, collectionOptions);
            }else{
                log.info("执行createCollection("+collectionName+");");
                mongoTemplate.createCollection(collectionName);
            }
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的createCollection()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    //地理索引--未实现
    public boolean createdGeoIndex(String collectionName, String indexName,
            String key, Integer max, Integer min) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的createdGeoIndex()中collectionName为null");
            return false;
        }
        try {
            System.out.println("===========createdGeoIndex=============");
            DefaultIndexOperations indexopera=new DefaultIndexOperations(mongoTemplate, collectionName);
            GeospatialIndex geoIndex=new GeospatialIndex(key) ;
            if(indexName!=null){
                geoIndex.named(indexName);
            }
            if ( max!= null) {
                geoIndex.withMax(max);
            }else {
                geoIndex.withMax(180);
            }
            if ( min!= null) {
                geoIndex.withMax(min);
            }else {
                geoIndex.withMax(-180);
            }
              
            indexopera.ensureIndex(geoIndex);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的createdGeoIndex()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
    /**
     *方法:为指定的集合指定的字段建立索引
     *
     * @param collectionName表示指定的集合;indexName表示指定索引的名称;key表示需要索引的字段;
     * duplicates表示是否为唯一索引,可以置为null默认为Index.Duplicates.RETAIN (保留),另一个是Index.Duplicates.DROP(删除);order表示升序还是降序,默认升序
     *@return boolean
     * **/
    public boolean createdIndex(String collectionName, String indexName,
            String key, Duplicates duplicates, Order order) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的createdIndex()中collectionName为null");
            return false;
        }
          
        try {
            DefaultIndexOperations indexopera=new DefaultIndexOperations(mongoTemplate, collectionName);
            Index index=new Index(key, order) ;
            index.named(indexName);
            if (duplicates!=null) {
                index.unique(duplicates);
            }
            indexopera.ensureIndex(index);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的createdIndex()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     *方法:删除指定集合中所有的索引
     *
     * @param collectionName表示指定的集合
     *@return boolean
     * **/
    public boolean dropAllIndexeByALL(String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的dropAllIndexeByALL()中collectionName为null");
            return false;
        }
        try {
            DefaultIndexOperations index=new DefaultIndexOperations(mongoTemplate, collectionName);
            index.dropAllIndexes();
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的dropAllIndexeByALL()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
      
    /**
     *方法:建立指定名称的集合
     *
     * @param collectionName表示指定的集合名称;collectionOptions表示建立集合的选项(CollectionOptions(Integer size, Integer maxDocuments, Boolean capped),size表示集合的存储空间单位为byte,maxDocuments表示最大文档数,capped表示是否为固定集合)
     *@return boolean
     * **/
    public boolean dropCollection(String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的dropCollection()中collectionName为null");
            return false;
        }
        try {
            mongoTemplate.dropCollection(collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的dropCollection()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
      
    /**
     *方法:删除指定的索引
     *
     * @param collectionName表示指定的集合;indexName表示指定索引的名称;
     *@return boolean
     * **/
    public boolean dropIndex(String collectionName, String indexName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的dropIndex()中collectionName为null");
            return false;
        }
        try {
            DefaultIndexOperations index=new DefaultIndexOperations(mongoTemplate, collectionName);
            index.dropIndex(indexName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的dropIndex()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
    /**
     *方法:判断当前数据库指定名称的集合是否存在
     *
     * @param 无
     *@return DBCollection
     * **/
    public  boolean collectionExists(String collectionName){
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的collectionExists()中collectionName为null");
            return false;
        }
        try {
            return mongoTemplate.collectionExists(collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的collectionExists()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     *方法:查找数据库中所有的数据
     *
     * @param classzz表示类的全称名
     *            (必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> findAll(String collectionName,Class classzz) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的findAll()中collectionName为null");
            return null;
        }
        try {
            return mongoTemplate.find(new Query(), classzz, collectionName);
            //return mongoTemplate.findAll(classzz, classzz.getName());// find(new
                                                                        // Query(),InformationVo.class);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的findAll()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
    /**
     *方法:分页查找数据库中所有的数据
     *
     * @param classzz表示类的全称名
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> findAllByPage(String collectionName,Class classzz, int startNum,
            int limit) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的findAllByPage()中collectionName为null");
            return null;
        }
        try {
            // 分页
            Query query = new Query();
            query.skip(startNum);
            query.limit(limit);
            return mongoTemplate.find(query, classzz,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的findAllByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:根据唯一条件精确查找指定的数据
     *
     * @param criteria表示指定的条件,criteria表示指定的条件
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public InformationVo findOne(String collectionName,Criteria criteria, Class classzz) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的findOne()中collectionName为null");
            return null;
        }
        try {
            // 分页
            Query query = new Query();
            query.addCriteria(criteria);
            query.limit(1);
            List<InformationVo> list=mongoTemplate.find(query, classzz,collectionName);
            if (list.size()>0) {
                return list.get(0);
            }else {
                return null;
            }
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的findOne()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:获取当前数据库指定名称的集合
     *
     * @param 无
     *@return DBCollection
     * **/
    public DBCollection getCollection(String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的getCollection()中collectionName为null");
            return null;
        }
        try {
            return mongoTemplate.getCollection(collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的getCollection()出现异常。");
            e.printStackTrace();
            return null;
        }
          
    }
      
    /**
     *方法:获取符合指定条件的文档数
     *
     * @param query表示查询条件;collectionName表示集合名
     *@return long(若返回-1.表示错误)
     * **/
    public long getCount(Query query, String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的getCount()中collectionName为null");
            return -1;
        }
        try {
            return mongoTemplate.count(query, collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的getCount()出现异常。");
            e.printStackTrace();
            return -1;
        }
          
    }
      
    /**
     *方法:获取当前数据库
     *
     * @param 无
     *@return DB
     * **/
    public DB getDb() {
        try {
            return mongoTemplate.getDb();
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的getDb()出现异常。");
            e.printStackTrace();
            return null;
        }
          
    }
      
    /**
     * 方法:向数据库批量插入对象
     *
     * @param list表示对象集合
     *            ,classzz表示类的全称名(必填)
     * @return boolean
     * **/
    //@SuppressWarnings("unchecked")
    public boolean insertList(List<? extends Object> list, String collectionName) {
        try {
            mongoTemplate.insert(list, collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的insertList()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     * 方法:向数据库插入一个对象
     *
     * @param obj任意对象
     *            (必填)
     * @return boolean
     * **/
    public boolean insertObject(Object obj,String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的insertObject()中collectionName为null");
            return false;
        }
        try {
            mongoTemplate.insert(obj, collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的insertObject()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
    /**
     *方法:分页查找数据库中以某点为中心且指定距离的附近数据
     *
     * @param point表示坐标;classzz表示类的全称名;maxDistance表示距离中心的最大距离;key表示关键字
     *            ,startNum表示起始数,limit表示个数
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> nearMaxDistance(String collectionName,Point point, Query query,
            Class classzz, Double maxDistance, int startNum, int limit) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的nearMaxDistance()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                query = new Query();
            }
            System.out.println("======MongoLBSImpl+++====="+maxDistance);
            if (maxDistance==null) {
                query.addCriteria(Criteria.where("infoMap.loc").near(point));
            }else {
                query.addCriteria(Criteria.where("infoMap.loc").near(point).maxDistance(Distance.getChange(maxDistance)));
            }
            //query.addCriteria(Criteria.where("infoMap.loc").near(point).maxDistance(maxDistance));
            /*if(key!=null){
                query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
            }*/
            query.skip(startNum);
            query.limit(limit);
            List<InformationVo> list=mongoTemplate.find(query, classzz,collectionName);
            if (list!=null) {
                for (InformationVo informationVo : list) {
                    //Double double1=Distance.GetDistance(point.getX(), point.getY(), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1));
                    informationVo.setDistance(Distance.GetDistance(point.getX(), point.getY(), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1)));
                    //System.out.println("MongoLBSImpl=====距离:"+double1);
                    //System.out.println("+++++MongoLBSImpl+++++++++"+point.getX()+"==="+ point.getY()+"==="+ ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0)+"==="+ ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1));
                }
                  
            }
            Collections.sort(list);
            return list;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的nearMaxDistance()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:分页查找数据库中以某点为中心且指定距离的附近数据
     *
     * @param lat表示纬度,lnt表示经度,classzz表示类的全称名,maxDistance表示距离中心的最大距离,key表示关键字
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> nearMaxDistance(String collectionName,double lat, double lnt,
            Query query, Class classzz, Double maxDistance, int startNum,
            int limit) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的nearMaxDistance()中collectionName为null");
            return null;
        }
        try {
            if (query==null) {
                query = new Query();
            }
            Point point=new Point(lat, lnt);
            if (maxDistance==null) {
                query.addCriteria(Criteria.where("infoMap.loc").near(point));
            }else {
                query.addCriteria(Criteria.where("infoMap.loc").near(point).maxDistance(Distance.getChange(maxDistance)));
            }
            /*if(key!=null){
                query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
            }*/
            query.skip(startNum);
            query.limit(limit);
            List<InformationVo> list=mongoTemplate.find(query, classzz,collectionName);
            if (list!=null) {
                for (InformationVo informationVo : list) {
                    //Double double1=Distance.GetDistance(point.getX(), point.getY(), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1));
                    informationVo.setDistance(Distance.GetDistance(point.getX(), point.getY(), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0), ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1)));
                    //System.out.println("MongoLBSImpl=====距离:"+double1);
                    //System.out.println("+++++MongoLBSImpl+++++++++"+point.getX()+"==="+ point.getY()+"==="+ ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(0)+"==="+ ((List<Double>)(informationVo.getInfoMap().get("loc"))).get(1));
                }
                  
            }
              
              
            Collections.sort(list);
            return list;
              
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的nearMaxDistance()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
      
    /**
     *方法:分页查找数据库中多边形内的数据
     *
     * @param centerX表示圆心的纬度,centerY表示圆心的经度,classzz表示类的全称名,radius表示半径
     *            ,startNum表示起始数,limit表示个数(必填)
     *@return List<InformationVo>
     * **/
    @SuppressWarnings("unchecked")
    public List<InformationVo> polygonByPage(String collectionName,Query query ,Class classzz, int startNum,
            int limit, Point x, Point y, Point z, Point... others) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的polygonByPage()中collectionName为null");
            return null;
        }
        try {
            if(query==null){
                query = new Query();
            }
            // 多边形
            Polygon polygon = new Polygon(x,y,z,others);
            query.addCriteria(Criteria.where("infoMap.loc").within(polygon));
            /*if(key!=null){
                query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
            }*/
            query.skip(startNum);
            query.limit(limit);
            return mongoTemplate.find(query, classzz,collectionName);
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的polygonByPage()出现异常。");
            e.printStackTrace();
            return null;
        }
    }
    /**
     *方法:删除符合指定条件的文档
     *
     * @param query表示更新条件,update表示更新内容(必填)
     *@return List<InformationVo>
     * **/
    public boolean remove(Query query,String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的remove()中collectionName为null");
            return false;
        }
        try {
            mongoTemplate.remove(query,collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的remove()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     *方法:删除未使用到指定集合的索引
     *
     * @param collectionName表示指定的集合
     *@return boolean
     * **/
    public boolean resetIndexCache(String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的resetIndexCache()中collectionName为null");
            return false;
        }
        try {
            DefaultIndexOperations index=new DefaultIndexOperations(mongoTemplate, collectionName);
            index.resetIndexCache();
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的resetIndexCache()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     * 方法:向数据库插入一个对象,若对象已经存在,则修改对象
     *
     * @param obj任意对象
     *            (必填)
     * @return boolean
     * **/
    public boolean saveObject(Object obj, String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的resetIndexCache()中collectionName为null");
            return false;
        }
        try {
            if (obj==null) {
                return false;
            }
            mongoTemplate.save(obj,collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的resetIndexCache()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     *方法:单个更新
     *
     * @param query表示更新条件,update表示更新内容(必填)
     *@return List<InformationVo>
     * **/
    public boolean updateFirst(Query query, Update update,String collectionName) {
        if (collectionName==null) {
            log.error("传给MongoLBSImpl类中的resetIndexCache()中collectionName为null");
            return false;
        }
        try {
            mongoTemplate.updateFirst(query, update, collectionName);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
      
    /**
     *方法:批量更新
     *
     * @param query表示更新条件,update表示更新内容(必填)
     *@return List<InformationVo>
     * **/
    public boolean updateMulti(Query query, Update update,String collectionName) {
        if (collectionName==null) {
            return false;
        }
        try {
            mongoTemplate.updateMulti(query, update, collectionName);
            return true;
        } catch (Exception e) {
            log.warn("传给MongoLBSImpl类中的resetIndexCache()出现异常。");
            e.printStackTrace();
            return false;
        }
    }
      
      
}