首页 > 代码库 > mongodb 创建LBS位置索引

mongodb 创建LBS位置索引

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>3.2.2</version>
</dependency>

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.4</version>
</dependency>


package com.teewoo.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* mongoDB 基本操作
*
* @author xiaojf  2016-6-24 15:55:56
*/
public class MongoBaseOps {
    private static MongoClient client;      //mongo客户端
    static {
        client = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017)));
    }

    /**
    * 根据db名称获取db对象
    *
    * @param name db名称
    * @return db对象
    * @author xiaojf  2016-6-24 16:27:56
    */
    public static MongoDatabase getDbByName(String name) {
        MongoDatabase mongoDatabase = client.getDatabase(name);
        return mongoDatabase;
    }

    /**
    * 根据mongo db名称和mongo collection 名称获取mongo collection对象
    *
    * @param dbName mongo db名称
    * @param collectionName mongo collection 名称
    * @return mongo collection对象
    * @author xiaojf  2016-6-24 16:36:39
    */
    public static MongoCollection getCollectionByName(String dbName,String collectionName){
        MongoCollection<Document> mongoCollection = getDbByName(dbName).getCollection(collectionName);

        return mongoCollection;
    }

    /**
    * 新增文档
    *
    * @param dbName mongo db 名称
    * @param collectionName mongo collection 名称
    * @param document 文档
    * @author xiaojf  2016-6-24 16:36:39
    */
    public static void insertDoc(String dbName,String collectionName,Document document){
        MongoCollection mongoCollection = getCollectionByName(dbName, collectionName);

        mongoCollection.insertOne(document);
    }

    /**
    * 批量新增文档
    *
    * @param dbName mongo db 名称
    * @param collectionName mongo collection 名称
    * @param documents 文档集合
    * @author xiaojf  2016-6-24 16:36:39
    */
    public static void batchInsertDoc(String dbName, String collectionName, List documents){
        MongoCollection mongoCollection = getCollectionByName(dbName, collectionName);

        mongoCollection.insertMany(documents);
    }

    /**
    * 根据id删除文档
    *
    * @param dbName mongo db 名称
    * @param collectionName mongo collection 名称
    * @param id mongo document _id
    * @author xiaojf  2016-6-24 17:07:16
    */
    public static void deleteById(String dbName, String collectionName,String id){
        if (StringUtils.isBlank(dbName) || StringUtils.isBlank(collectionName) || StringUtils.isBlank(id)) {
            return ;
        }
        MongoCollection mongoCollection = getCollectionByName(dbName, collectionName);
        mongoCollection.deleteOne(new BasicDBObject("_id", new ObjectId(id)));
    }

    /**
    * 根据id获取文档
    *
    * @param dbName mongo db 名称
    * @param collectionName mongo collection 名称
    * @param id mongo document _id
    * @author xiaojf  2016-6-24 17:07:16
    */
    public static Object getById(String dbName, String collectionName,String id){
        if (StringUtils.isBlank(dbName) || StringUtils.isBlank(collectionName) || StringUtils.isBlank(id)) {
            return null;
        }

        MongoCollection mongoCollection = getCollectionByName(dbName, collectionName);
        FindIterable findIterable = mongoCollection.find(new BasicDBObject("_id", new ObjectId(id)));
        MongoCursor cursor = findIterable.iterator();

        while (cursor.hasNext()) {
            return cursor.next();
        }

        return null;
    }

    /**
    * 创建索引
    *
    * @param dbName mongo db 名称
    * @param collectionName mongo collection 名称
    * @param fieldName 字段名称
    * @param indexType 索引类型
    * @author xiaojf  2016-6-27 13:43:02
    */
    public static void createIndex(String dbName, String collectionName,String fieldName,String indexType){
        MongoCollection mongoCollection = getCollectionByName(dbName, collectionName);
        mongoCollection.createIndex(new BasicDBObject(fieldName, indexType));
    }

    public static void main(String[] args) {
        //获取 mongodb db对象
        MongoDatabase mongoDatabase = MongoBaseOps.getDbByName("db_test");
        //删除旧mongodb collection
        mongoDatabase.getCollection("collect_gps").drop();

        List<Document> documentList = new ArrayList<Document>();
        for (int i = 0 ;i<100;i++) {
            //构建2d索引的LBS document
            Document doc = new Document();
            doc.put("gps", BasicDBObjectBuilder.start().add("lon", 33+i).add("lat", i-33).get());
            documentList.add(doc);
        }
        //批量新增文档给
        MongoBaseOps.batchInsertDoc("db_test","collect_gps",documentList);
        //创建2d索引
        MongoBaseOps.createIndex("db_test","collect_gps","gps","2d");
    }
}

 

mongodb 创建LBS位置索引