The JVM SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
JVM - collection.query.where()
Adds a new where clause to a query, which filters the data returned.
import io.nitric.Nitric;
import io.nitric.resources.CollectionPermission;
import io.nitric.api.documents.v0.Operator;
// A user class to store in the profiles collection
class User {
String name;
int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Application {
public static void main(String[] args) {
var profiles = Nitric.INSTANCE.collection("profiles", User.class).with(CollectionPermission.Read);
var profilesQuery = profiles.query().where("name", Operator.STARTS_WITH, "T");
NITRIC.INSTANCE.run();
}
}
Parameters
- Name
field
- Required
- Required
- Type
- String
- Description
The document field to query.
- Name
operation
- Required
- Required
- Type
- Operator
- Description
The query operation to perform.
Valid values are:
STARTS_WITH | EQ | NEQ | GTE | LTE | GT | LT
.
- Name
value
- Required
- Required
- Type
- String or Int or Boolean
- Description
The value to compare against.
Where clauses combined together are always considered
AND
Examples
A simple query
import io.nitric.Nitric;
import io.nitric.resources.CollectionPermission;
import io.nitric.api.documents.v0.Operator;
// A user class to store in the profiles collection
class User {
String name;
int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Application {
public static void main(String[] args) {
var profiles = Nitric.INSTANCE.collection("profiles", User.class).with(CollectionPermission.Read);
var profilesQuery = profiles.query().where("name", Operator.STARTS_WITH, "T");
NITRIC.INSTANCE.run();
}
}
Combining where clauses
var profileQuery = profiles
.query()
.where("firstName", Operator.EQ, "Drake")
.where("age", Operator.GTE, 21)