Querying with Select

The examples in this section query a document store and retrieve specific fields from the documents.

Note: Selecting a specific field is also known as a projection. You can improve the performance of projection queries by using secondary indexes. See Using Indexes to Optimize Projections in Queries for more details.

The following example shows how to retrieve the _id and address.zipCode fields from all documents in a store using the Query.select method.

 * 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,
 * 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_006_FindQueryWithSelect {

  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
    final Query query = connection.newQuery()
        .select("_id", "address.zipCode")

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

    for (final Document userDocument : stream) {
      // Print the OJAI Document

    // Close this instance of OJAI DocumentStore

    // close the OJAI connection and release any resources held by the connection

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


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

# mapr dbshell
maprdb root:> find /demo_table --query {"$select":["_id","address.zipcode"]}

maprdb root:> find /demo_table --fields _id,address.zipcode