【工具类】JAVA使用MongoDB

1 篇文章 0 订阅
1 篇文章 0 订阅

我的原则:先会用再说,内部慢慢来。
学以致用,根据场景学源码


一、Mongo版本3.6.3

  • pom.xml
  <properties>
      <mongo.version>3.6.3</mongo.version>
  </properties>
    
 <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>${mongo.version}</version>
        </dependency>

        <dependency>
            <artifactId>mongo-java-driver</artifactId>
            <groupId>org.mongodb</groupId>
            <version>${mongo.version}</version>
        </dependency>
 </dependencies>     

二、Mongo配置文件

  • mongodb.properties
# --> prod <--
#mongodb.authenticationDatabase=database
#mongodb.dbname=dbname
#mongodb.username=username
#mongodb.password=password
#mongodb.ip_port=ip1:port,ip2:port,ip3:port

#  --> dev <--
mongodb.authenticationDatabase=database
mongodb.dbname=dbname
mongodb.username=username
mongodb.password=password
mongodb.ip_port=127.0.0.1:27017

三、初始化

3.1 ConfigPropertyUtil 工具类

  • 用于获取 properties中的value

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * 用于获取可配置的变量的工具类
 */
public final class ConfigPropertyUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigPropertyUtil.class);

    /**
     * @param propertiesPath 配置文件所在地址
     * @param propertyName   变量名
     *                       从配置文件中获取指定的变量
     */
    public static String getProperty(String propertiesPath,
                                     String propertyName) {
        Properties prop = new Properties();

        InputStreamReader inputStreamReader = null;
        try {

            InputStream in = ConfigPropertyUtil.class.getClassLoader().getResourceAsStream(propertiesPath);
            if (null == in) {
                LOGGER.error("propertiesPath -> {} not exist .", propertiesPath);
                return null;
            }
            inputStreamReader = new InputStreamReader(in,"UTF-8");
            prop.load(inputStreamReader);

            // getObject the property value and print it out
            return prop.getProperty(propertyName);

        } catch (IOException ex) {
            LOGGER.error("ex -> {}", ex);
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    LOGGER.error("e -> {}", e);
                }
            }
        }
        return null;
    }
}

3.2 MongoOperatorEnum


/**
 * MongoOperatorEnum
 *
 * @author jeb_lin
 * 3:36 PM 26/10/2018
 */
public enum MongoOperatorEnum {

    /**
     * =========== Comparison Query Operators ===========
     **/
    /*
      Matches values that are equal to a specified value.
    */
    Equal("$eq"),
    /*
        Matches values that are greater than a specified value.
     */
    GreaterThan("$gt"),
    /*
        Matches values that are greater than or equal to a specified value.
     */
    GreaterThanOrEqual("$gte"),

    /*
        Matches any of the values specified in an array.
     */
    In("$in"),

    /*
         Matches values that are less than a specified value.
     */
    LessThan("$lt"),
    /*
        Matches values that are less than or equal to a specified value.
     */
    LessThanOrEqual("$lte"),
    /*
         Matches all values that are not equal to a specified value.
     */
    NotEqual("$ne"),
    /*
         Matches none of the values specified in an array.
     */
    NIn("$nin"),

    /**
     * =========== Logical Query Operators ===========
     **/

    /*
        Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
     */
    And("$and"),
    /*
        Inverts the effect of a query expression and returns documents that do not match the query expression.
     */
    Not("$not"),
    /*
        Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
     */
    Nor("$nor"),
    /*
        Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
     */
    Or("$or"),

    /**
     * =========== Element Query Operators ===========
     **/

    /*
        Matches documents that have the specified field.
     */
    Exist("$exists"),

    /*
        Selects documents if a field is of the specified type.
     */
    Type("$type"),

    /**
     * =========== Evaluation Query Operators ===========
     **/

    /*
        Allows use of aggregation expressions within the query language.
     */
    Expr("$expr"),
    /*
        Validate documents against the given JSON Schema.
     */
    JsonSchema("$jsonSchema"),
    /*
        Performs a modulo operation on the value of a field and selects documents with a specified result.
     */
    Mod("$mod"),

    /*
        Selects documents where values match a specified regular expression.
     */
    Regex("$regex"),

    /*
        Performs text search.
     */
    Text("$text"),
    /*
        Matches documents that satisfy a JavaScript expression.
     */
    Where("$where"),

    /**
     * =========== Array Query Operators ===========
     **/
    /*
        Matches arrays that contain all elements specified in the query.
     */
    All("$all"),

    /*
        Selects documents if element in the array field matches all the specified $elemMatch conditions.
     */
    ElemMatch("$elemMatch"),

    /*
        Selects documents if the array field is a specified size
     */
    Size("$size"),

    /**
     * =========== Geo Operators ===========
     **/
    NearSphere("$nearSphere"),
    GeoWithin ("$geoWithin"),
    GeoIntersects("$geoIntersects"),
    geoNear("$geoNear"),
    Box("$box"),
    Polygon("$polygon"),
    Center("$center"),
    CenterSphere("$centerSphere"),
    Geometry("$geometry");


    private final String key;

    MongoOperatorEnum(String key) {
        this.key = key;
    }

    public String key() {
        return this.key;
    }


}

3.3 MongoDBUtil 初始化配置


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mongodb.*;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.mongodb.internal.validator.CollectibleDocumentFieldNameValidator;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.*;

/**
 * MongoDB工具类 Mongo实例代表了一个数据库连接池,即使在多线程的环境中,一个Mongo实例对我们来说已经足够了<br>
 * 注意Mongo已经实现了连接池,并且是线程安全的。 <br>
 * 设计为单例模式, 因 MongoDB的Java驱动是线程安全的,对于一般的应用,只要一个Mongo实例即可,<br>
 * Mongo有个内置的连接池(默认为10个) 对于有大量写和读的环境中,为了确保在一个Session中使用同一个DB时,<br>
 * DB和DBCollection是绝对线程安全的<br>
 *
 * @author jeb_lin
 * <p>
 * 6:42 PM 03/04/2018
 */
public enum MongoDBUtil {

    /**
     * 定义一个枚举的元素,它代表此类的一个实例
     */
    instance;

    private MongoClient mongoClient;

    private static final Logger LOGGER = LoggerFactory.getLogger(MongoDBUtil.class);

    private static final String PROPERTIES_PATH = "mongodb.properties";
    private static final String PROPERTIESKEY_IP_PORT_MONGODB = "mongodb.ip_port";
    private static final String PROPERTIESKEY_USERNAME = "mongodb.username";
    private static final String PROPERTIESKEY_PASSWORD = "mongodb.password";
    private static final String PROPERTIESKEY_DBNAME = "mongodb.dbname";
    private static final String PROPERTIESKEY_DBNAME_PROD = "mongodb.dbname.prod";
    private static final String PROPERTIESKEY_AUTHENTICATION_DBNAME = "mongodb.authenticationDatabase";

    private static final String IP_PORT_MONGODB = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_IP_PORT_MONGODB);
    private static final String USERNAME = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_USERNAME);
    private static final String PASSWORD = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_PASSWORD);
    private static final String DBNAME = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_DBNAME);
    private static final String DBNAME_PROD = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_DBNAME_PROD);
    private static final String AUTHENTICATION_DBNAME = ConfigPropertyUtil.getProperty(PROPERTIES_PATH, PROPERTIESKEY_AUTHENTICATION_DBNAME);

    private static final String MONGO_OBJECT_ID_KEY_NAME = "_id";

    static {
        LOGGER.info("===============MongoDBUtil init start========================");

        // Enable MongoDB logging in general
        System.setProperty("DEBUG.MONGO", "true");
        // Enable DB operation tracing
        System.setProperty("DB.TRACE", "true");

        List<ServerAddress> mongoHostList = Lists.newArrayList();
        // 可能是一个mongo集群
        if (null != IP_PORT_MONGODB) {
            String[] ipPorts = IP_PORT_MONGODB.split(",");
            for (String ipPort : ipPorts) {
                String ip = ipPort.split(":")[0];
                int port = Integer.parseInt(ipPort.split(":")[1]);
                mongoHostList.add(new ServerAddress(ip, port));
            }
        } else {
            LOGGER.error("ipPorts can not be null ,IP_PORT_MONGODB");
        }

        LOGGER.info("===============MongoDB connection over ========================");

        /*
         *    大部分用户使用mongodb都在安全内网下,但如果将mongodb设为安全验证模式,就需要在客户端提供用户名和密码:
         */
        MongoClientOptions mongoClientOptions = MongoClientOptions.builder()
                // 连接池设置为300个连接,默认为100
                .connectionsPerHost(50)
                // 线程队列数,如果连接线程排满了队列就会抛出“Out of semaphores to getObject db”错误。
                .threadsAllowedToBlockForConnectionMultiplier(500)
                // 连接超时,推荐>3000毫秒
                .connectTimeout(30000)
                // 套接字超时时间,0无限制
                .socketTimeout(30000)
                .maxWaitTime(60000)
                .build();

        // 假如有 USERNAME 和 PASSWORD 和 DBNAME
        if (!StringUtils.isEmpty(USERNAME) && !StringUtils.isEmpty(PASSWORD) && !StringUtils.isEmpty(AUTHENTICATION_DBNAME)) {
            MongoCredential credential = MongoCredential.createCredential(USERNAME, AUTHENTICATION_DBNAME, PASSWORD.toCharArray());
            instance.mongoClient = new MongoClient(mongoHostList, credential, mongoClientOptions);
        } else {
            instance.mongoClient = new MongoClient(mongoHostList);
        }
    }

    public static void init() {
        LOGGER.info("init the MongoDBUtil ...");
    }

四、通用方法

// ------------------------------------共用方法---------------------------------------------------

    /**
     * 获取DB实例 - 指定DB
     *
     * @author jeb_lin
     * <p>
     * 6:43 PM 03/04/2018
     */
    public MongoDatabase getDB(String dbName) {
        if (dbName != null && !"".equals(dbName)) {
            return mongoClient.getDatabase(dbName);
        }
        return null;
    }

    /**
     * 获取collection对象 - 指定Collection
     *
     * @author jeb_lin
     * <p>
     * 6:43 PM 03/04/2018
     */
    public MongoCollection<Document> getCollection(String dbName, String collName) {
        if (null == collName || "".equals(collName)) {
            return null;
        }
        if (null == dbName || "".equals(dbName)) {
            return null;
        }
        return mongoClient.getDatabase(dbName).getCollection(collName);
    }


    @Deprecated
    public MongoCollection<Document> getCollection(String collName) {
        if (null == collName || "".equals(collName)) {
            return null;
        }
        if (StringUtils.isEmpty(DBNAME)) {
            LOGGER.error("DBNAME can not be null");
            return null;
        }
        return mongoClient.getDatabase(DBNAME).getCollection(collName);
    }

    public MongoCollection<Document> getCollectionProd(String collName) {
        if (null == collName || "".equals(collName)) {
            return null;
        }
        if (StringUtils.isEmpty(DBNAME_PROD)) {
            LOGGER.error("DBNAME can not be null");
            return null;
        }
        return mongoClient.getDatabase(DBNAME_PROD).getCollection(collName);
    }

    /**
     * 查询DB下的所有表名
     *
     * @author jeb_lin
     * <p>
     * 6:43 PM 03/04/2018
     */
    public List<String> getAllCollections(String dbName) {
        MongoIterable<String> colls = getDB(dbName).listCollectionNames();
        List<String> list = new ArrayList<>();
        for (String s : colls) {
            list.add(s);
        }
        return list;
    }

    /**
     * 获取所有数据库名称列表
     *
     * @author jeb_lin
     * <p>
     * 6:43 PM 03/04/2018
     */
    public MongoIterable<String> getAllDBNames() {
        return mongoClient.listDatabaseNames();
    }

    /**
     * 删除一个数据库
     *
     * @author jeb_lin
     * <p>
     * 6:44 PM 03/04/2018
     */
    public void dropDB(String dbName) {
        getDB(dbName).drop();
    }

	public void dropCollection(String dbName, String collName) {
        getDB(dbName).getCollection(collName).drop();
    }

    /**
     * 关闭Mongodb
     */
    public void close() {
        if (mongoClient != null) {
            mongoClient.close();
            mongoClient = null;
        }
    }

五、CRUD

5.1 统计

 /**
     * 统计数量
     *
     * @author jeb_lin
     * <p>
     * 3:52 PM 21/09/2018
     */
    public long size(MongoCollection<Document> coll) {
        return size(coll, null);
    }

    public long size(MongoCollection<Document> coll, Map<String, Object> filterMap) {
        if (null == coll) {
            return 0;
        }
        Document query = new Document();

        if (null != filterMap) {
            for (Map.Entry<String, Object> entry :
                    filterMap.entrySet()) {
                query.put(entry.getKey(), entry.getValue());
            }
        }

        return coll.count(query);
    }

5.2 参数拼接

/**
     * 拼接参数,比如和查询 age 是 11 还有 22的人
     * or
     *
     * @author jeb_lin
     * 11:09 PM 11/07/2018
     */
    public Document jointORParam(Object... param) {
        return new Document(MongoOperatorEnum.In.key(), Lists.newArrayList(param));
    }

    public Document jointNINParam(Object... param) {
        return new Document(MongoOperatorEnum.NIn.key(), Lists.newArrayList(param));
    }

    public Document beforeIOSDate(long time) {
        return new Document(MongoOperatorEnum.LessThan.key(), new Timestamp(time));
    }

    public Document queryByIOSDate(MongoOperatorEnum operatorEnum, long time) {
        return new Document(operatorEnum.key(), new Timestamp(time));
    }

5.3 插入

public void insert(MongoCollection<Document> coll, Document Document) {
        if (null == coll) {
            return;
        }

        try {
            coll.insertOne(Document);
        } catch (java.lang.IllegalArgumentException e) {
            Document newDocument = removeTheIllegalKeyForStore(Document);
            coll.insertOne(newDocument);
        }
    }

5.4 查找

/**
     * 查找对象 - 根据主键_id
     *
     * @author jeb_lin
     * <p>
     * 6:44 PM 03/04/2018
     */
    public Document findById(MongoCollection<Document> coll, String id) {
        if (null == coll) {
            return null;
        }
        try {
            ObjectId objectId = new ObjectId(id);
            return coll.find(Filters.eq(MONGO_OBJECT_ID_KEY_NAME, objectId)).first();
        } catch (Exception e) {
            return null;
        }
    }

public Document findOneAndUpdate(MongoCollection<Document> coll,
                                     Map<String, Object> filterMap, Map<String, Object> updateMap) {
        if (null == coll) {
            return null;
        }

        Document query = new Document();

        if (null != filterMap) {
            for (Map.Entry<String, Object> entry :
                    filterMap.entrySet()) {
                query.put(entry.getKey(), entry.getValue());
            }
        }


        Document updateDocument = new Document();
        for (Map.Entry<String, Object> entry :
                updateMap.entrySet()) {
            updateDocument.put(entry.getKey(), entry.getValue());
        }
        Document setDocument = new Document("$set", updateDocument);

        return coll.findOneAndUpdate(query, setDocument);
    }

    /**
     * 分页查询
     *
     * @author jeb_lin
     * <p>
     * 6:44 PM 03/04/2018
     */
    public MongoCursor<Document> findByPage(MongoCollection<Document> coll, Bson filter, int pageNo, int pageSize) {
        if (null == coll) {
            return null;
        }
        Bson orderBy = new Document(MONGO_OBJECT_ID_KEY_NAME, 1);
        return coll.find(filter).sort(orderBy).skip((pageNo - 1) * pageSize).limit(pageSize).iterator();
    }

    /**
     * @param coll         collection
     * @param filterMap    filter
     * @param sortFieldMap -1 表示倒序
     * @param fieldNames   fileNames with include or not
     * @param include      or exclude
     * @param index        skip index
     * @param limit        -1 表示全部
     *                     根据条件查询出指定的列
     * @author jeb_lin
     * <p>
     * 2:00 PM 21/08/2018
     */
    public MongoCursor<Document> findSortAndLimit(MongoCollection<Document> coll,
                                                  Map<String, Object> filterMap,
                                                  List<String> fieldNames,
                                                  boolean include,
                                                  Map<String, Integer> sortFieldMap,
                                                  int index,
                                                  int limit) {
        if (null == coll) {
            return null;
        }

        Document query = new Document();
        if (null != filterMap) {
            for (Map.Entry<String, Object> entry :
                    filterMap.entrySet()) {
                query.put(entry.getKey(), entry.getValue());
            }
        }
        Bson projectionBson = null;
        if (fieldNames != null && !fieldNames.isEmpty()) {
            if (include) {
                projectionBson = Projections.include(fieldNames);
            } else {
                projectionBson = Projections.exclude(fieldNames);
            }
        }


        if (sortFieldMap != null && !sortFieldMap.isEmpty()) {
            Document sortQuery = new Document();
            for (Map.Entry<String, Integer> entry :
                    sortFieldMap.entrySet()) {
                sortQuery.put(entry.getKey(), entry.getValue());
            }

            if (fieldNames != null && !fieldNames.isEmpty()) {
                if (limit != -1) {
                    return coll.find(query).projection(projectionBson).sort(sortQuery).skip(index).limit(limit).iterator();
                } else {
                    return coll.find(query).projection(projectionBson).sort(sortQuery).skip(index).iterator();
                }

            } else {
                if (limit != -1) {
                    return coll.find(query).sort(sortQuery).skip(index).limit(limit).iterator();
                } else {
                    return coll.find(query).sort(sortQuery).skip(index).iterator();
                }
            }

        } else {

            if (null != fieldNames && !fieldNames.isEmpty()) {
                if (limit != -1) {
                    return coll.find(query).projection(projectionBson).skip(index).limit(limit).iterator();
                } else {
                    return coll.find(query).projection(projectionBson).skip(index).iterator();
                }

            } else {
                if (limit != -1) {
                    return coll.find(query).skip(index).limit(limit).iterator();
                } else {
                    return coll.find(query).skip(index).iterator();
                }
            }


        }
    }

    public MongoCursor<Document> findSort(MongoCollection<Document> coll,
                                          Map<String, Object> filterMap,
                                          Map<String, Integer> sortFieldMap) {
        return findSortAndLimit(coll, filterMap, null, true, sortFieldMap, 0, -1);
    }

    public MongoCursor<Document> findSortWithLimit(MongoCollection<Document> coll,
                                                   Map<String, Object> filterMap,
                                                   Map<String, Integer> sortFieldMap,
                                                   int limit) {
        return findSortAndLimit(coll, filterMap, null, true, sortFieldMap, 0, limit);
    }

    public MongoCursor<Document> findSortWithIndex(MongoCollection<Document> coll,
                                                   Map<String, Object> filterMap,
                                                   Map<String, Integer> sortFieldMap,
                                                   int index) {
        return findSortAndLimit(coll, filterMap, null, true, sortFieldMap, index, -1);
    }

    public MongoCursor<Document> findSortWithIndexLimit(MongoCollection<Document> coll,
                                                        Map<String, Object> filterMap,
                                                        Map<String, Integer> sortFieldMap,
                                                        int index,
                                                        int limit) {
        return findSortAndLimit(coll, filterMap, null, true, sortFieldMap, index, limit);
    }

    public MongoCursor<Document> findSortWithFiledIndexLimit(MongoCollection<Document> coll,
                                                             Map<String, Object> filterMap,
                                                             List<String> fieldNames,
                                                             boolean include,
                                                             Map<String, Integer> sortFieldMap,
                                                             int index,
                                                             int limit) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, sortFieldMap, index, limit);
    }

    public MongoCursor<Document> findSortWithFiledIndex(MongoCollection<Document> coll,
                                                        Map<String, Object> filterMap,
                                                        List<String> fieldNames,
                                                        boolean include,
                                                        Map<String, Integer> sortFieldMap,
                                                        int index) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, sortFieldMap, index, -1);
    }

    public MongoCursor<Document> findSortWithFiledLimit(MongoCollection<Document> coll,
                                                        Map<String, Object> filterMap,
                                                        List<String> fieldNames,
                                                        boolean include,
                                                        Map<String, Integer> sortFieldMap,
                                                        int limit) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, sortFieldMap, 0, limit);
    }

    public MongoCursor<Document> findSortWithFiled(MongoCollection<Document> coll,
                                                   Map<String, Object> filterMap,
                                                   List<String> fieldNames,
                                                   boolean include,
                                                   Map<String, Integer> sortFieldMap) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, sortFieldMap, 0, -1);
    }

    public MongoCursor<Document> find(MongoCollection<Document> coll, Bson filter) {
        return coll.find(filter).iterator();
    }

    public MongoCursor<Document> find(MongoCollection<Document> coll, Map<String, Object> filterMap) {
        return findSortAndLimit(coll, filterMap, null, true, null, 0, -1);
    }

    public MongoCursor<Document> findOne(MongoCollection<Document> coll, Map<String, Object> filterMap) {
        return findSortAndLimit(coll, filterMap, null, true, null, 0, 1);
    }

    public MongoCursor<Document> findWithLimit(MongoCollection<Document> coll, Map<String, Object> filterMap, int limit) {
        return findSortAndLimit(coll, filterMap, null, true, null, 0, limit);
    }

    public MongoCursor<Document> findWithIndex(MongoCollection<Document> coll, Map<String, Object> filterMap, int index) {
        return findSortAndLimit(coll, filterMap, null, true, null, index, -1);
    }

    public MongoCursor<Document> findWithIndexLimit(MongoCollection<Document> coll, Map<String, Object> filterMap, int index, int limit) {
        return findSortAndLimit(coll, filterMap, null, true, null, index, limit);
    }

    public MongoCursor<Document> findWithFiled(MongoCollection<Document> coll,
                                               Map<String, Object> filterMap,
                                               List<String> fieldNames,
                                               boolean include) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, null, 0, -1);
    }


    public MongoCursor<Document> findWithFiledIndex(MongoCollection<Document> coll,
                                                    Map<String, Object> filterMap,
                                                    List<String> fieldNames,
                                                    boolean include,
                                                    int index) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, null, index, -1);
    }

    public MongoCursor<Document> findWithFiledLimit(MongoCollection<Document> coll,
                                                    Map<String, Object> filterMap,
                                                    List<String> fieldNames,
                                                    boolean include,
                                                    int limit) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, null, 0, limit);
    }

    public MongoCursor<Document> findWithFiledIndexLimit(MongoCollection<Document> coll,
                                                         Map<String, Object> filterMap,
                                                         List<String> fieldNames,
                                                         boolean include,
                                                         int index,
                                                         int limit) {
        return findSortAndLimit(coll, filterMap, fieldNames, include, null, index, limit);
    }

    /**
     * 找寻全部
     *
     * @author jeb_lin
     * <p>
     * 6:58 PM 03/04/2018
     */
    public MongoCursor<Document> findAll(MongoCollection<Document> coll) {
        if (null == coll) {
            return null;
        }
        return coll.find().iterator();
    }

5.5 删除

 /**
     * 通过ID删除
     *
     * @author jeb_lin
     * <p>
     * 6:44 PM 03/04/2018
     */
    public long deleteById(MongoCollection<Document> coll, String id) {
        if (null == coll) {
            return 0;
        }
        ObjectId objectId;
        try {
            objectId = new ObjectId(id);
        } catch (Exception e) {
            return 0;
        }
        Bson filter = Filters.eq("objectId", objectId);
        DeleteResult deleteResult = coll.deleteOne(filter);
        return deleteResult.getDeletedCount();
    }


    /**
     * filterMap.put("interests",new BasicDBObject("$ne",110));
     *
     * @author jeb_lin
     * <p>
     * 2:49 PM 21/08/2018
     */
    public long delete(MongoCollection<Document> coll, Map<String, Object> filterMap, boolean batchDelete) {
        if (null == coll) {
            return 0;
        }
        Document query = new Document();
        if (null != filterMap) {
            for (Map.Entry<String, Object> entry :
                    filterMap.entrySet()) {
                query.put(entry.getKey(), entry.getValue());
            }
        }
        DeleteResult deleteResult;
        if (batchDelete) {
            deleteResult = coll.deleteMany(query);
        } else {
            deleteResult = coll.deleteOne(query);
        }
        return deleteResult.getDeletedCount();
    }

    public long deleteAll(MongoCollection<Document> coll) {
        if (null == coll) {
            return 0;
        }
        return delete(coll, null, true);
    }

5.6 更新

private Document getBasicDBObjectByFilterMap(Map<String, Object> filterMap) {
        Document query = new Document();
        if (null != filterMap) {
            for (Map.Entry<String, Object> entry :
                    filterMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();

                query.put(key, value);

                if (MONGO_OBJECT_ID_KEY_NAME.equals(key)) {
                    try {
                        ObjectId objectId = new ObjectId(String.valueOf(value));
                        // id 是唯一的
                        query.put(MONGO_OBJECT_ID_KEY_NAME, objectId);
                        break;
                    } catch (Exception e) {
                        LOGGER.error("{}", e);
                        return null;
                    }
                }
            }
        }
        return query;
    }
    
public long update(MongoCollection<Document> coll, Map<String, Object> filterMap,
                       Map<String, Object> updateMap, boolean batchUpdate) {
        if (null == coll) {
            return 0;
        }
        Document query = getBasicDBObjectByFilterMap(filterMap);

        if (null == updateMap || updateMap.isEmpty() || null == query) {
            return 0;
        }
        Document updateDocument = new Document();
        for (Map.Entry<String, Object> entry :
                updateMap.entrySet()) {
            updateDocument.put(entry.getKey(), entry.getValue());
        }

        UpdateResult updateResult;
        Document setDocument = new Document("$set", updateDocument);
        if (batchUpdate) {
            updateResult = coll.updateMany(query, setDocument);
        } else {
            updateResult = coll.updateOne(query, setDocument);
        }
        return updateResult.getModifiedCount();
    }

/**
     * 通过ID更新
     *
     * @author jeb_lin
     * <p>
     * 6:44 PM 03/04/2018
     */
    public Document updateById(MongoCollection<Document> coll, String id, Document newdoc) {
        if (null == coll) {
            return null;
        }
        ObjectId objectId;
        try {
            objectId = new ObjectId(id);
        } catch (Exception e) {
            return null;
        }
        Bson filter = Filters.eq(MONGO_OBJECT_ID_KEY_NAME, objectId);
        // coll.replaceOne(filter, newdoc); // 完全替代
        coll.updateOne(filter, new Document("$set", newdoc));
        return newdoc;
    }

5.7 数值加一 INC

public long inc(MongoCollection<Document> coll, Map<String, Object> filterMap,
                    String incName, boolean batchInc) {
        return inc(coll, filterMap, incName, 1, batchInc);
    }

    public long inc(MongoCollection<Document> coll, Map<String, Object> filterMap,
                    String incName, int incCount, boolean batchInc) {
        if (null == coll) {
            return 0;
        }
        Document query = getBasicDBObjectByFilterMap(filterMap);
        if (StringUtils.isEmpty(incName) || null == query) {
            return 0;
        }
        Document updateDocument = new Document();
        updateDocument.put(incName, incCount);

        UpdateResult updateResult;
        Document setDocument = new Document("$inc", updateDocument);
        if (batchInc) {
            updateResult = coll.updateMany(query, setDocument);
        } else {
            updateResult = coll.updateOne(query, setDocument);
        }
        return updateResult.getModifiedCount();
    }

5.8 去除非法key

  • 开发过程中遇到某些key保存的时候报错,于是写了以下方法 :
public static Document removeTheIllegalKeyForStore(Document document) {
        /*
            1、 new document
         */
        Document newDocument = new Document();

        for (Map.Entry<String, Object> entry :
                document.entrySet()) {

            Object entryValue = entry.getValue();
            if (null == entryValue) {
                continue;
            }
            Class<?> clazz = entryValue.getClass();

            /*
                2、 if the entryValue class is Map,
                validate the Map key
             */
            if (Map.class.isAssignableFrom(clazz)) {

                Map<Object, Object> map = (Map<Object, Object>) entryValue;
                Map<Object, Object> newMap = Maps.newHashMap();

                /*
                    3. get the Map key type
                 */
                Class<?> keyClazz = null;
                Set<Object> keySet = map.keySet();
                for (Object k : keySet) {
                    keyClazz = k.getClass();
                    break;//只需要判断第一个元素
                }
                /*
                    4. if the Map key type is "java.lang.String"
                 */
                if ("java.lang.String".equals(keyClazz.getName())) {
                    for (Map.Entry<Object, Object> keyNameEntry :
                            map.entrySet()) {
                        String realKeyName = String.valueOf(keyNameEntry.getKey());
                        boolean isValidate = new CollectibleDocumentFieldNameValidator().validate(realKeyName);

                        /*
                            5. validate
                         */
                        if (isValidate) {
                            newMap.put(keyNameEntry.getKey(), keyNameEntry.getValue());
                        } else {
                            LOGGER.error("realKeyName -> {} is illegal ... remove it", realKeyName);
                        }
                    }
                    entryValue = newMap;
                }
            }
            newDocument.put(entry.getKey(), entryValue);
        }
        return newDocument;
    }

    public static Object removeTheIllegalKeyForStore(Class clazz, Object instance) {
        /*
            1. find the field which type is Map<String,?>
         */
        List<Field> mapFieldList = Lists.newArrayList();
        Field[] fields = clazz.getDeclaredFields();
        if (null != fields) {
            for (Field field :
                    fields) {
                Class<?> fieldClazz = field.getType();
                if (Map.class.isAssignableFrom(fieldClazz) &&
                        ParameterizedTypeImpl.class.isAssignableFrom(field.getGenericType().getClass())) {
                    ParameterizedTypeImpl parameterizedTypeImpl = (ParameterizedTypeImpl) field.getGenericType();
                    Type keyType = parameterizedTypeImpl.getActualTypeArguments()[0];
                    if (keyType.getTypeName().equalsIgnoreCase("java.lang.String")) {
                        System.out.println(field.getName());
                        mapFieldList.add(field);
                    }
                }
            }
        }
        /*
            2. validate the map key whether it can be store
         */
        for (Field mapField :
                mapFieldList) {
            try {
                mapField.setAccessible(true);
                Map<String, Object> map = (Map<String, Object>) mapField.get(instance);
                Set<String> illegalKeys = Sets.newHashSet();
                for (String key :
                        map.keySet()) {
                    boolean isValidate = new CollectibleDocumentFieldNameValidator().validate(key);

                    if (!isValidate) {
                        illegalKeys.add(key);
                    }
                }
                for (String illegalKey :
                        illegalKeys) {
                    LOGGER.error("remove illegalKey -> {}", illegalKey);
                    map.remove(illegalKey);
                }
                mapField.set(instance, map);
            } catch (IllegalAccessException e) {
                LOGGER.error("{}", e);
            }
        }

        return instance;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值