You can use the find method to issue a query to retrieve data from a collection in MongoDB. All queries in MongoDB have the scope of a single collection. Queries can return all documents in a collection or only the documents that match a specified filter or criteria. You can specify the filter or criteria in a org.bson.Document and pass as a parameter to the find method.
The find method returns query results in a FindIterable, which is an iterable object that yields documents.
The examples in this section use the restaurants collection in the test database. For instructions on populating the collection with the sample dataset, see Import Example Dataset. Follow the Connect to MongoDB step to connect to a running MongoDB instance and declare and define the variable db to access the test database. Include the following import statements:
To return all documents in a collection, call the find method without a criteria document. For example, the following operation queries for all documents in the restaurants collection.
Specify Equality Conditions
With the MongoDB Java driver, use the following code to implement the equalities conditions document:
The following operation finds documents whose borough field equals "Manhattan".
To specify a condition on a field within an embedded document, use the dot notation. Dot notation requires quotes around the whole dotted field name. The following operation specifies an equality condition on the zipcode field in theaddress embedded document.
Query by a Field in an Array
The grades array contains embedded documents as its elements. To specify a condition on a field in these documents, use the dot notation. Dot notation requires quotes around the whole dotted field name. The following queries for documents whose grades array contains an embedded document with a field grade equal to "B".
Specify Conditions with Operators
MongoDB provides operators to specify query conditions, such as comparison operators. Although there are some exceptions, such as the $or and $and conditional operators, query conditions using operators generally have the following form:
Query for documents whose grades array contains an embedded document with a field score greater than 30.
Query for documents whose grades array contains an embedded document with a field score less than 10.
You can combine multiple query conditions in logical conjunction (AND) and logical disjunctions (OR).
You can specify a logical conjunction (AND) for multiple query conditions by appending conditions to the query document. See the append method in the org.bson.Document class.
You can specify a logical disjunction (OR) for a list of query conditions by using the $or query operator.
To specify an order for the result set, append the sort() method to the query. Pass to sort() method a document which contains the field(s) to sort by and the corresponding sort type, e.g. 1 for ascending and -1 for descending.
For example, the following operation returns all documents in the restaurants collection, sorted first by the borough field in ascending order, and then, within each borough, by the "address.zipcode" field in ascending order:
In the Java Driver documentation, see find, Filters, FindIterable, Block, and sort; In the MongoDB Manual, see also Query Documents tutorial, Projection tutorial, Query and Projection Operators reference, and Cursor Methods.
* Getting Started - Insert Data with Java Driver
* Getting Started - Update Data with Java Driver