All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cybermkd.kit.MongoQuery Maven / Gradle / Ivy

There is a newer version: 1.0.8.2
Show newest version
package com.cybermkd.kit;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBRef;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.TextSearchOptions;
import com.mongodb.client.model.Updates;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

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

/**
 * 创建人:T-baby
 * 创建日期: 16/4/15
 * 文件描述:方便增删查改
 */
public class MongoQuery {

    private String collectionName;
    private String join;
    private Document document = new Document();
    private List documents = new ArrayList();
    private List query = new ArrayList();
    private List data = new ArrayList();
    private Bson sort;
    private Bson projection;
    /*用于记录单挑插入时的id*/
    private String id;
    private int limit = 0;
    private int skip = 0;

    public String getCollectionName() {
        return collectionName;
    }

    public int getSkip() {
        return skip;
    }

    public int getLimit() {
        return limit;
    }

    public Bson getSort() {
        return sort;
    }

    public MongoQuery use(String name) {
        this.collectionName = name;
        return this;
    }

    public static Bson and(List query) {
        return query.size() == 0 ? new BsonDocument() : Filters.and((Iterable) query);
    }

    public MongoQuery or(MongoQuery query) {
        query.add(or(query.getQuery()));
        return this;
    }

    public static Bson or(List query) {
        return query.size() == 0 ? new BsonDocument() : Filters.or((Iterable) query);
    }

    public MongoQuery nor(MongoQuery query) {
        query.add(nor(query.getQuery()));
        return this;
    }

    public static Bson nor(List query) {
        return query.size() == 0 ? new BsonDocument() : Filters.nor((Iterable) query);
    }

    public MongoQuery join(String key, String collectionName, String id) {
        DBRef ref = new DBRef(collectionName, new ObjectId(id));
        document.append(key, ref);
        return this;
    }

    public MongoQuery join(String key) {
        this.join = key;
        return this;
    }

    public Document getDocument() {
        return this.document;
    }

    public List getDocuments() {
        return this.documents;
    }

    public List getQuery() {
        return this.query;
    }

    public String getId() {
        return id;
    }

    public List data() {
        return this.data;
    }

    public MongoQuery set(String key, Object value) {
        document.append(key, value);
        return this;
    }

    /*支持java bean*/
    public MongoQuery set(Object obj) {
        document = Document.parse(JSON.toJSONString(obj));
        return this;
    }

    public MongoQuery add(MongoQuery query) {
        documents.add(query.getDocument());
        return this;
    }

    /*用于支持更多原生方法*/
    public MongoQuery add(Bson bson) {
        data.add(bson);
        return this;
    }

    public MongoQuery filter(Bson filter) {
        query.add(filter);
        return this;
    }

    public MongoQuery eq(String key, Object value) {
        query.add(Filters.eq(key, value));
        return this;
    }

    public MongoQuery ne(String key, Object value) {
        query.add(Filters.ne(key, value));
        return this;
    }

    public MongoQuery regex(String key, String regex) {
        query.add(Filters.regex(key, regex));
        return this;
    }

    public MongoQuery type(String key, String type) {
        query.add(Filters.type(key, type));
        return this;
    }


    public MongoQuery mod(String key, long divisor, long remainder) {
        query.add(Filters.mod(key, divisor, remainder));
        return this;
    }


    public MongoQuery text(String search) {
        query.add(Filters.text(search));
        return this;
    }

    public MongoQuery text(String search, TextSearchOptions textSearchOptions) {
        query.add(Filters.text(search, textSearchOptions));
        return this;
    }

    public MongoQuery where(String javaScriptExpression) {
        query.add(Filters.where(javaScriptExpression));
        return this;
    }

    public MongoQuery elemMatch(String key, MongoQuery query) {
        this.query.add(Filters.elemMatch(key, this.and(query.getQuery())));
        return this;
    }

    public MongoQuery size(String key, int size) {
        query.add(Filters.size(key, size));
        return this;
    }

    //支持查询id
    public MongoQuery in(String key, List values) {
        if ("_id".equals(key)) {
            List idList = new ArrayList();
            values.forEach(value -> {
                idList.add(new ObjectId(String.valueOf(value)));
            });
            query.add(Filters.in(key, idList));
        } else {
            query.add(Filters.in(key, values));
        }

        return this;
    }


    public MongoQuery gt(String key, Object value) {
        query.add(Filters.gt(key, value));
        return this;
    }

    public MongoQuery lt(String key, Object value) {
        query.add(Filters.lt(key, value));
        return this;
    }

    public MongoQuery gte(String key, Object value) {
        query.add(Filters.gte(key, value));
        return this;
    }

    public MongoQuery lte(String key, Object value) {
        query.add(Filters.lte(key, value));
        return this;
    }

    public MongoQuery modify(String key, MongoQuery query) {
        this.modify(key, query.getDocument());
        return this;
    }

    public MongoQuery modify(Object obj) {
        JSONObject json = (JSONObject) JSON.toJSON(obj);
        for (String key : json.keySet()) {
            if (json.getString(key) != null && !json.getString(key).isEmpty()) {
                this.modify(key, json.getString(key));
            }
        }
        return this;
    }

    public MongoQuery modify(String key, Object value) {
        data.add(Updates.set(key, value));
        return this;
    }

    public MongoQuery inc(String key, Number value) {
        data.add(Updates.inc(key, value));
        return this;
    }


    public MongoQuery like(String key, String value) {
        Pattern pattern = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
        query.add(Filters.regex(key, pattern));
        return this;
    }

    //1为以什么开头,2为以什么结尾
    public MongoQuery like(int type, String key, String value) {

        if (type == 1) {
            Pattern pattern = Pattern.compile("^" + value + ".*$", Pattern.CASE_INSENSITIVE);
            query.add(Filters.regex(key, pattern));
        } else {
            Pattern pattern = Pattern.compile("^.*" + value + "$", Pattern.CASE_INSENSITIVE);
            query.add(Filters.regex(key, pattern));
        }
        return this;
    }

    public MongoQuery byId(String id) {
        query.add(new Document("_id", new ObjectId(id)));
        return this;
    }

    public long save() {
        long row = MongoKit.INSTANCE.insert(collectionName, document);
        this.id = this.document.getObjectId("_id").toString();
        document.clear();
        return row;
    }

    public long saveList() {
        long row = MongoKit.INSTANCE.insert(collectionName, documents);
        documents.clear();
        return row;
    }


    public MongoQuery projection(String... keys) {
        for (String key : keys) {
            this.projection = new BasicDBObject().append(key, 1);
        }
        return this;
    }


    public MongoQuery limit(int i) {
        this.limit = i;
        return this;
    }

    public MongoQuery skip(int i) {
        this.skip = i;
        return this;
    }


    public List findAll() {
        return MongoKit.INSTANCE.find(collectionName, limit, skip, sort, projection, join);
    }

    public  List findAll(Class clazz) {
        return MongoKit.INSTANCE.find(collectionName, limit, skip, sort, projection, join, clazz);
    }


    public JSONObject findOne() {
        return MongoKit.INSTANCE.findOne(collectionName, and(query), sort, join);
    }

    public  T findOne(Class clazz) {
        return MongoKit.INSTANCE.findOne(collectionName, and(query), sort, join, clazz);
    }

    public List find() {
        return MongoKit.INSTANCE.find(collectionName, and(query), sort, projection, limit, skip, join);
    }

    public  List find(Class clazz) {
        return MongoKit.INSTANCE.find(collectionName, and(query), sort, projection, limit, skip, join, clazz);
    }

    public MongoQuery ascending(String... keys) {
        this.sort = Sorts.ascending(Arrays.asList(keys));
        return this;
    }

    public MongoQuery descending(String... keys) {
        this.sort = Sorts.descending(Arrays.asList(keys));
        return this;
    }

    public long count() {
        return MongoKit.INSTANCE.count(collectionName, and(query));
    }

    public JSONObject max(String key) {
        descending(key);
        return findOne();
    }

    public  T max(String key, Class clazz) {
        descending(key);
        return findOne(clazz);
    }

    public JSONObject min(String key) {
        ascending(key);
        return findOne();
    }

    public  T min(String key, Class clazz) {
        ascending(key);
        return findOne(clazz);
    }


    /*存在某个key*/
    public MongoQuery exist(String key) {
        set(Filters.exists(key));
        return this;
    }

    /*判断某个值是否存在*/
    public boolean exist() {
        return this.count() > 0;
    }

    public long update() {
        return MongoKit.INSTANCE.update(collectionName, and(query), Updates.combine(data));
    }

    public long updateOne() {
        return MongoKit.INSTANCE.updateOne(collectionName, and(query), Updates.combine(data));
    }

    public long delete() {
        return MongoKit.INSTANCE.delete(collectionName, and(query));
    }

    public long deleteOne() {
        return MongoKit.INSTANCE.deleteOne(collectionName, and(query));
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy