Example usage for com.mongodb QueryBuilder is

List of usage examples for com.mongodb QueryBuilder is

Introduction

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

Prototype

public QueryBuilder is(final Object object) 

Source Link

Document

Equivalent of the find({key:value})

Usage

From source file:org.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

protected QueryBuilder buildLikeQuery(Like obj, QueryBuilder query) {
    if (obj.isNegated()) {
        query.not();/* www. jav  a 2 s  . co  m*/
    }

    append(obj.getRightExpression());

    StringBuilder value = new StringBuilder((String) this.onGoingExpression.pop());
    int idx = -1;
    while (true) {
        idx = value.indexOf("%", idx + 1);//$NON-NLS-1$
        if (idx != -1 && idx == 0) {
            continue;
        }
        if (idx != -1 && idx == value.length() - 1) {
            continue;
        }

        if (idx == -1) {
            break;
        }
        value.replace(idx, idx + 1, ".*"); //$NON-NLS-1$
    }

    if (value.charAt(0) != '%') {
        value.insert(0, '^');
    }

    idx = value.length();
    if (value.charAt(idx - 1) != '%') {
        value.insert(idx, '$');
    }

    String regex = value.toString().replaceAll("%", ""); //$NON-NLS-1$ //$NON-NLS-2$
    query.is(Pattern.compile(regex));
    return query;
}