Querying with Conditions

The examples in this section query a document store and return documents that have specific conditions.

For more information about how to specify query conditions in OJAI, see Query Conditions in OJAI Applications.

Note: You can improve the performance of queries with conditions by using secondary indexes. See Queries that Benefit from Secondary Indexes for more details.

The following example shows how to return all documents from a store where address.zipCode equals 95196, using the Query.where method. It uses an OJAI QueryCondition to specify the condition.

/**
 * Copyright (c) 2017 MapR, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapr.ojai.examples;

import org.ojai.Document;
import org.ojai.DocumentStream;
import org.ojai.store.Connection;
import org.ojai.store.DocumentStore;
import org.ojai.store.DriverManager;
import org.ojai.store.Query;
import org.ojai.store.QueryCondition.Op;

public class OJAI_007_FindQueryWithCondition {

  public static void main(final String[] args) {

    System.out.println("==== Start Application ===");


    // Create an OJAI connection to MapR cluster
    final Connection connection = DriverManager.getConnection("ojai:mapr:");

    // Get an instance of OJAI DocumentStore
    final DocumentStore store = connection.getStore("/demo_table");

    // Build an OJAI query with QueryCondition
    final Query query = connection.newQuery()
        .where(
            connection.newCondition()                   //
              .is("address.zipCode", Op.EQUAL, 95196)   // Build an OJAI QueryCondition
              .build())                                 //
        .build();

    // fetch all OJAI Documents from this store
    final DocumentStream stream = store.findQuery(query);

    for (final Document userDocument : stream) {
      // Print the OJAI Document
      System.out.println(userDocument.asJsonString());      
    }

    // Close this instance of OJAI DocumentStore
    store.close();

    // close the OJAI connection and release any resources held by the connection
    connection.close();

    System.out.println("==== End Application ===");
  }

}

The following example shows how to return all documents from a store where address.zipCode equals 95196, using the Query.where method. It specifies the query condition using a JSON string.

/**
 * Copyright (c) 2017 MapR, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapr.ojai.examples;

import org.ojai.Document;
import org.ojai.DocumentStream;
import org.ojai.store.Connection;
import org.ojai.store.DocumentStore;
import org.ojai.store.DriverManager;
import org.ojai.store.Query;

public class OJAI_008_FindQueryWithConditionJson {

  public static void main(final String[] args) {

    System.out.println("==== Start Application ===");


    // Create an OJAI connection to MapR cluster
    final Connection connection = DriverManager.getConnection("ojai:mapr:");

    // Get an instance of OJAI DocumentStore
    final DocumentStore store = connection.getStore("/demo_table");

    // Build an OJAI query with the condition specified as a JSON string
    final Query query = connection.newQuery()
        .where("{\"$eq\": {\"address.zipCode\": 95196}}")
        .build();

    // fetch all OJAI Documents from this store
    final DocumentStream stream = store.findQuery(query);

    for (final Document userDocument : stream) {
      // Print the OJAI Document
      System.out.println(userDocument.asJsonString());      
    }

    // Close this instance of OJAI DocumentStore
    store.close();

    // close the OJAI connection and release any resources held by the connection
    connection.close();

    System.out.println("==== End Application ===");
  }

}

The following two dbshell commands are equivalent to the code examples. See dbshell find or findbyid for more details about the syntax dbshell provides.

# mapr dbshell
maprdb root:> find /demo_table --q {"$where":{"$eq":{"address.zipCode":95196}}}

maprdb root:> find /demo_table --where {"$eq":{"address.zipCode":95196}}