Querying with Conditions
You can apply conditions to queries by using QueryCondition
objects
and then passing those objects to the find()
or findById()
methods. All filtering, except for filtering on expressions, takes place in MapR-DB before
results are passed back to client applications.
Creating a QueryCondition
object
Suppose that you have a JSON table named Movies that contains documents about movies. You want to query the table to find all of the romantic comedy movies that were released in the year 2015 by a particular movie studio.
You could build a query condition like this:
public QueryCondition buildQueryCondition() {
return MapRDB.newCondition()
.and()
.is("studio", Op.EQUAL, "RomComs Unltd.")
.is("release_date", Op.GREATER_OR_EQUAL, Values.parseDate("2015-01-01"))
.is("release_date", Op.LESS_OR_EQUAL, Values.parseDate("2015-12-31"))
.close()
.build();
}
The .and()
requires that all of the conditions set by the nested
.is()
specifications must be true. The enum Op
says to
compare the value in the filter with the value in each document for the given field. The
keyword after Op
specifies the type of comparison to make.
QueryCondition
objects are
described further in the OJAI documentation.
Returning multiple whole documents by querying with conditions
Call the find()
method of a Table
object that represents
the table. When calling the method, pass a QueryCondition
object.
The result is a DocumentStream
object, which is a collection of
Document
objects that contain the returned whole OJAI documents.
public void findDoc(String tablePath, QueryCondition condition) {
try(Table table = MapRDB.getTable(tablePath);
DocumentStream documentStream = table.find(condition)) {
for(Document document : documentStream) {
System.out.println(document);
}
}
}
Returning multiple partial documents by querying with conditions
Call the find()
method of a Table
object that represents
the table. When calling the method, pass a QueryCondition
object and the
path or paths of the fields to include in the returned documents.
The result is a DocumentStream
object, which is a list of
Document
objects that contain the returned partial OJAI documents.
public void findDoc(String tablePath, QueryCondition condition, String[] fieldPaths) {
try(Table table = MapRDB.getTable(tablePath);
DocumentStream documentStream = table.find(condition, fieldPaths)) {
for(Document document : documentStream) {
System.out.println(document);
}
}
}
Returning single whole documents by querying with conditions
Call the findById()
method of a Table
object that
represents the table. When calling the method, pass the value of the _id
field in the document and a QueryCondition
object.
The result is a Document
object that contains the returned whole JSON
document.
public void findDocById(String tablePath, String _id, QueryCondition condition) {
try(Table table = MapRDB.getTable(tablePath)) {
System.out.println(table.findById(_id, condition));
}
}
Returning single partial documents by querying with conditions
Call the findById()
method of a Table
object that
represents the table. When calling the method, pass the value of the _id
field in the document, a QueryCondition
object, and the paths of the fields
that you want to include in the returned document.
The result is a Document
object that contains the returned partial JSON
document.
public void findDocById(String tablePath, String _id, QueryCondition condition, String fieldPath) {
try(Table table = MapRDB.getTable(tablePath)) {
System.out.println(table.findById(_id, condition, fieldPath));
}
}