Example usage for com.mongodb QueryBuilder regex

List of usage examples for com.mongodb QueryBuilder regex

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder regex.

Prototype

public QueryBuilder regex(final Pattern regex) 

Source Link

Document

Passes a regular expression for a query

Usage

From source file:com.naryx.tagfusion.expression.function.string.serializejson.java

License:Open Source License

public void encodeJSON(StringBuilder out, cfData var, boolean serializeQueryByColumns, CaseType caseConversion,
        DateType datetype) throws dataNotSupportedException {

    if (var == null || var.getDataType() == cfData.CFNULLDATA) {
        out.append("null");

    } else if (var.getDataType() == cfData.CFSTRINGDATA) {

        /*/*  w  ww .j  av  a  2 s  . co  m*/
         * Encode Strings
         */
        out.append("\"" + encodeString(var.getString()) + "\"");

    } else if (var.getDataType() == cfData.CFBOOLEANDATA) {

        /*
         * Boolean Data
         */
        out.append(var.getBoolean() ? "true" : "false");

    } else if (var.getDataType() == cfData.CFDATEDATA) {

        /*
         * Encode Date
         */
        Date localeDate = (Date) ((cfDateData) var).getUnderlyingInstance();

        if (datetype == DateType.LONG)
            out.append("\"" + dateLongFormat.format(localeDate) + "\"");
        else if (datetype == DateType.HTTP)
            out.append("\"" + dateHTTPFormat.format(localeDate) + "\"");
        else if (datetype == DateType.JSON)
            out.append("\"" + dateJSONFormat.format(localeDate) + "\"");
        else if (datetype == DateType.MONGO)
            out.append("{$date:\"" + dateMongoFormat.format(localeDate) + "\"}");
        else
            out.append("\"" + var.getString() + "\"");

    } else if (cfData.isSimpleValue(var)) {

        /*
         * Encode numbers and booleans
         */
        out.append(var.getString());

    } else if (var.getDataType() == cfData.CFARRAYDATA) {

        /*
         * Encode Arrays
         */
        out.append("[");

        cfArrayData cfarraydata = (cfArrayData) var;
        for (int x = 0; x < cfarraydata.size(); x++) {
            encodeJSON(out, cfarraydata.getElement(x + 1), serializeQueryByColumns, caseConversion, datetype);

            if (x < cfarraydata.size() - 1)
                out.append(",");
        }
        out.append("]");

    } else if (var.getDataType() == cfData.CFSTRUCTDATA) {

        /*
         * Encode Structs
         */
        out.append("{");

        cfStructData cfstructdata = (cfStructData) var;
        Object[] keys = cfstructdata.keys();
        String key, value = null;
        for (int x = 0; x < keys.length; x++) {
            key = (String) keys[x];

            if (caseConversion == CaseType.LOWER)
                value = key.toLowerCase();
            else if (caseConversion == CaseType.UPPER)
                value = key.toUpperCase();
            else if (caseConversion == CaseType.MAINTAIN)
                value = key;

            out.append("\"" + encodeString(value) + "\"");
            out.append(":");
            encodeJSON(out, cfstructdata.getData(key), serializeQueryByColumns, caseConversion, datetype);

            if (x < keys.length - 1)
                out.append(",");
        }

        out.append("}");

    } else if (var.getDataType() == cfData.CFQUERYRESULTDATA) {

        /*
         * Encode Query
         */
        out.append("{");

        if (serializeQueryByColumns)
            encodeQueryByColumnsJSON(out, (cfQueryResultData) var, serializeQueryByColumns, caseConversion,
                    datetype);
        else
            encodeQueryByRowsJSON(out, (cfQueryResultData) var, serializeQueryByColumns, caseConversion,
                    datetype);

        out.append("}");

    } else if (var.getDataType() == cfData.CFJAVAOBJECTDATA) {
        Object o = ((cfJavaObjectData) var).getUnderlyingInstance();
        if (o instanceof org.bson.types.ObjectId)
            out.append("{$oid:\"" + ((org.bson.types.ObjectId) o).toString() + "\"}");
        else if (o instanceof java.util.regex.Pattern) {

            QueryBuilder qb = new QueryBuilder();
            qb.put("tmp");
            qb.regex((java.util.regex.Pattern) o);
            String json = qb.get().toString();
            json = json.substring(json.indexOf("{", 1), json.lastIndexOf("}"));
            out.append(json);

        } else
            out.append("\"" + encodeString(String.valueOf(o)) + "\"");
    } else {
        out.append("\"unsupported\"");
    }
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

private void addQueryCriterion(final SearchCriterion searchCriterion, final QueryBuilder queryBuilder) {

    final ComparisonOperator comparisonOperator = searchCriterion.getComparisonOperator();
    final Object comparisonValue = searchCriterion.getComparisonValue();
    switch (comparisonOperator) {

    case containsAll: {
        queryBuilder.all(comparisonValue);
        break;//from w  ww  .  j  a  v a  2s. c  o  m
    }

    case equalTo: {
        queryBuilder.equals(comparisonValue);
        break;
    }

    case equalToAny: {
        queryBuilder.in(comparisonValue);
        break;
    }

    case exists: {
        queryBuilder.exists(true);
        break;
    }

    case greaterThan: {
        queryBuilder.greaterThan(comparisonValue);
        break;
    }

    case greaterThanOrEqualTo: {
        queryBuilder.greaterThanEquals(comparisonValue);
        break;
    }

    case lessThan: {
        queryBuilder.lessThan(comparisonValue);
        break;
    }

    case lessThanOrEqualTo: {
        queryBuilder.lessThanEquals(comparisonValue);
        break;
    }

    case notEqualTo: {
        queryBuilder.notEquals(comparisonValue);
        break;
    }

    case notEqualToAny: {
        queryBuilder.notIn(comparisonValue);
        break;
    }

    case notExists: {
        queryBuilder.exists(false);
        break;
    }

    case regex: {
        final Pattern regexPattern = searchCriterion.getRegexPattern();
        if (regexPattern != null) {
            queryBuilder.regex(regexPattern);
        }

        break;
    }

    }
}