java中操作Mongodb

Posted 风茂angel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中操作Mongodb相关的知识,希望对你有一定的参考价值。

Mongodb是一个nosql型的数据库,存储的是类似于json对象,能够支持类似于关系型数据库的一些操作。以下是java操作mongodb。

一、建立连接(没有使用任何框架,连接是自己写的,同时在这个类写了关闭连接的方法),传入ip地址、端口和数据库名称(mongodb称集合)

public class MongoDbConn {
    private static MongoClient mongoClient = null;
    private static MongoDatabase mongoDatabase = null;
    public static MongoDatabase getDatabase(String ipaddress,int port,String dbName) {
        mongoClient = new MongoClient(ipaddress, port);
        mongoDatabase = mongoClient.getDatabase(dbName);
        return mongoDatabase;
    }
    public static void closeDatabase(){
        try{
            if(mongoClient!=null){
                mongoClient.close();
            }
        }catch(Exception e){
            throw new RuntimeException();
        }finally{
            mongoClient = null;
        }
        
    
    }

}

二、使用操作,以下是查询的例子

public List<Data> getAllAbsData(RankParam rankParam) {
        List<Data> dataList = new ArrayList<Data>();
        MongoDatabase mongoDatabase = MongoDbConn.getDatabase("localhost", 27017, "area_new");
        MongoCollection<Document> collection = mongoDatabase.getCollection("vaabs_monthly");
        FindIterable<Document> findIterable = null;
        if ("vaabs".equals(rankParam.getIndexCode())) {
            if (rankParam.getAreaCode() == null || "".equals(rankParam.getAreaCode())) {// 全国
                //db.vaabs_monthly.find({"$and":[{"indusId":300},{"areaCode":/.*0000$/}]})
                Pattern queryPattern = Pattern.compile(".*0000$", Pattern.CASE_INSENSITIVE);
                BasicDBObject queryObject = new BasicDBObject();
                queryObject.put("areaCode", queryPattern);
                queryObject.put("indusId", rankParam.getIndusId());
                findIterable = collection.find(queryObject);
                
            }else {//全省所有市的数据
                //db.vaabs_monthly.find({"$and":[{"indusId":300},{"date":"2007-01-31"},{"areaCode":/^13/},{"areaCode":/.*00$/},{"areaCode":{"$not":/.*0000$/}}]})
                //Pattern startPattern = Pattern.compile("^13", Pattern.CASE_INSENSITIVE);
                Pattern startPattern = Pattern.compile("^"+rankParam.getAreaCode(), Pattern.CASE_INSENSITIVE);
                Pattern endPattern = Pattern.compile(".*00$", Pattern.CASE_INSENSITIVE);
                Pattern notPattern = Pattern.compile(".*0000$", Pattern.CASE_INSENSITIVE);
                BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND, new BasicDBObject[]{new BasicDBObject("indusId",rankParam.getIndusId()),
                        new BasicDBObject("areaCode",startPattern),new BasicDBObject("areaCode",endPattern),new BasicDBObject("areaCode", new BasicDBObject("$not",notPattern))
                        });
                
                findIterable = collection.find(queryObject);
                
                
            }
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while (mongoCursor.hasNext()) {
                Document document = mongoCursor.next();
                dataList.add(new Data(document.getString("name"), document.getDouble("value"), document.getString("date"), document.getString("areaCode"), document.getInteger("indusId")));
            }

        }else{
            
        }
        return dataList;
        
    }

 

以上是关于java中操作Mongodb的主要内容,如果未能解决你的问题,请参考以下文章

Java操作MongoDB的增删改查,完整项目分享

Java操作MongoDB的增删改查,完整项目分享

在 Spring MongoDB 的 ReplaceRoot 管道阶段使用 $mergeObjects

Java操作MongoDB增删改查完美Demo

java对mongodb数据库的简单操作

四.MongoDB入门-Java操作MongoDB