Query MongoDB - Java SDK
On this page
- Use Cases
- Prerequisites
- Set Up Your Project
- Set Up Your Project
- Link a MongoDB Atlas Service Cluster
- Import Realm Dependencies
- Instantiate a MongoDB Collection Handle
- Example Data
- Create Documents
- Insert a Single Document
- Insert Multiple Documents
- Read Documents
- Find a Single Document
- Find Multiple Documents
- Count Documents in the Collection
- Update Documents
- Update a Single Document
- Update Multiple Documents
- Upsert Documents
- Delete Documents
- Delete a Single Document
- Delete Multiple Documents
- Watch for Changes
- Watch for Changes in a Collection
- Watch for Changes in a Collection with a Filter
- Aggregate Documents in a Collection
- Filter Documents
- Group Documents
- Project Document Fields
- Add Fields to Documents
- Unwind Array Values
You can query data stored in MongoDB Atlas directly from your Android application code by using the Realm Java SDK's MongoClient with the Query API. Atlas App Services provides data access rules on collections to securely retrieve results based on the logged-in user or the content of each document.
The following actions enable access to a linked MongoDB Atlas cluster from an Android application using the Realm SDK.
Note
Each operation described on this page uses a query to
match certain documents in the collection upon which the operation
executes. When a filter matches multiple documents in a collection,
they are returned in an indeterminate order unless you
specify a sorting parameter. This means that if you do not specify
a sort for the findOne()
, updateOne()
, or deleteOne()
functions, your operation could match any document that matches the
query. For more information on sorting, see
cursor.sort().
Use Cases
There are a variety of reasons you might want to query a MongoDB data source. Working with data in your client via Atlas Device Sync is not always practical or possible. You might want to query MongoDB when:
The data set is large or the client device has constraints against loading the entire data set
You are retrieving documents that are not modeled in Realm
Your app needs to access collections that don't have strict schemas
A non-Realm service generates collections that you want to access
While not exhaustive, these are some common use cases for querying MongoDB directly.
Prerequisites
Before you can query MongoDB from your Android application, you must set up MongoDB Data Access in your App Services App. To learn how to set up your backend App to let the Realm SDK query Atlas, refer to Set Up MongoDB Data Access in the App Services documentation.
Set Up Your Project
Set Up Your Project
Follow the steps in the Install the Realm Java SDK guide.
Link a MongoDB Atlas Service Cluster
Follow the steps in the Link a MongoDB data source guide. Assign your service a meaningful name -- you'll need it to connect to the cluster using the Realm SDK.
Instantiate a MongoDB Collection Handle
To connect to an instance of MongoDB, you'll need a user with access to a MongoDB collection. Log in to your application as such a user, then use the following code to instantiate a local MongoDB collection handle.
Note
Using Custom Classes with MongoDB
To use classes other than the built-in Document
class with
MongoDB, you can add codecs to your MongoCollection
instances.
In the above example, we add the PojoCodecProvider
to support
Plain Old Java Objects (POJOs). Custom object support requires two
codec providers:
the default codec provider, which provides support for built-in Java types (accessed through
AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY
)PojoCodecProvider
, which automatically creates new codecs to support POJO classes
The SDK checks registries in order until one returns a codec for
the requested class. As a result, you should list the default codec
registry first, and the PojoCodecProvider
should always be the
last CodecProvider since it can provide a codec for almost any
class.
Example Data
The following examples operate on a MongoDB collection that describes inventory in a chain of plant stores. Consider the following collection of documents describing various plants for sale in a store:
Create Documents
These code snippets demonstrate how to insert one or more documents into a MongoDB collection from a mobile application. Insert operations take a document or documents to add to MongoDB as an argument and return a RealmResultTask that resolves to an object that contains the results of the execution of the operation.
Insert a Single Document
You can insert a single document using collection.insertOne().
The following snippet inserts a single document describing a "lily of the valley" plant into a collection of documents that describe plants for sale in a group of stores:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully inserted a document with id: BsonObjectId{value=5f19...}
Insert Multiple Documents
You can insert multiple documents at the same time using collection.insertMany().
The following snippet inserts three documents describing plants into a collection of documents that describe plants for sale in a group of stores:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully inserted 3 documents into the collection.
Read Documents
These code snippets demonstrate how to read data stored in a MongoDB
collection from a mobile application. Read operations use queries to specify which documents to
return from the database. Read operations return a
Task that resolves to
either a single matched document (in the case of findOne()
), a long
numeric value (in the case of count()
) or an iterator that allows you to
traverse the collection of matched documents (in the case of find()
).
Find a Single Document
You can find a single document using collection.findOne().
The following snippet finds a single document from the a
collection of documents that describe plants for sale in a group of stores where the plant document's type
field contains the string value "perennial":
Running this snippet produces output similar to the following:
V/EXAMPLE: successfully found a document: Plant [id=5f18..., name=venus flytrap, sunlight=full, color=white, type=perennial, partition=Store 42]
Find Multiple Documents
You can find multiple documents using collection.find().
The following snippet finds all documents in a
collection of documents that describe plants for sale in a group of stores that contain a field named
_partition
with a value of "Store 42":
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully found all plants for Store 42: V/EXAMPLE: Plant [id=5f18..., name=venus flytrap, sunlight=full, color=white, type=perennial, partition=Store 42] V/EXAMPLE: Plant [id=5f18..., name=sweet basil, sunlight=partial, color=green, type=annual, partition=Store 42] V/EXAMPLE: Plant [id=5f18..., name=thai basil, sunlight=partial, color=green, type=perennial, partition=Store 42] V/EXAMPLE: Plant [id=5f18..., name=helianthus, sunlight=full, color=yellow, type=annual, partition=Store 42]
Count Documents in the Collection
You can count documents in a collection using collection.count(). You can specify an optional query to determine which documents to count. If you don't specify a query, the action counts all documents in the collection.
The following snippet counts the number of documents in a collection of documents that describe plants for sale in a group of stores:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully counted, number of documents in the collection: 5
Update Documents
These code snippets demonstrate how to update data stored in a MongoDB collection from a mobile application. Update operations use queries to specify which documents to update and update operators to describe how to mutate documents that match the query. Update operations return a Task that resolves to an object that contains the results of the execution of the operation.
Update a Single Document
You can update a single document using collection.updateOne().
The following snippet updates a single document in a
collection of documents that describe plants for sale in a group of stores. This operation queries for a document
where the name
field contains the value "petunia" and changes the value
of the first matched document's sunlight
field to "partial":
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully updated a document.
Update Multiple Documents
You can update multiple documents using collection.updateMany().
The following snippet updates multiple documents in a
collection of documents that describe plants for sale in a group of stores. This operation queries for documents
where the _partition
field contains the value "Store 47" and changes
the value of the _partition
field of each matching document to "Store 51":
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully updated 2 documents.
Upsert Documents
If an update operation does not match any document in the collection,
you can automatically insert a single new document into the collection
that matches the update query by setting the upsert
option to
true
.
The following snippet updates a document in a collection of documents that describe plants for sale in a group of stores or inserts a new document if no document matches the query. This operation queries for documents where:
the
sunlight
field has a value of "full"the
type
field has a value of "perennial"the
color
field has a value of "green"the
_partition
field has a value of "Store 47"
Because this snippet sets the upsert
option to true
, if no
document matches the query, MongoDB creates a new document that includes
both the query and specified updates:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully upserted a document with id: BsonObjectId{value=5f19...}
Delete Documents
These code snippets demonstrate how to delete documents that are stored in a MongoDB collection from a mobile application. Delete operations use a query to specify which documents to delete and return a Task that resolves to an object that contains the results of the execution of the operation.
Delete a Single Document
You can delete a single document from a collection using collection.deleteOne().
The following snippet deletes one document in a
collection of documents that describe plants for sale in a group of stores. This operation queries for a
document where the color
field has a value of "green" and deletes
the first document that matches the query:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully deleted a document.
Delete Multiple Documents
You can delete multiple items from a collection using collection.deleteMany().
The following snippet deletes all documents in a
collection of documents that describe plants for sale in a group of stores
that match the query that matches documents containing both
a sunlight
field value of "full" and a type
field value of
"annual".
Running this snippet produces output resembling the following:
V/EXAMPLE: succcessfully deleted 2 documents.
Watch for Changes
These code snippets demonstrate how to configure and run watch operations on a collection.
Important
Serverless Limitations
You cannot watch for changes if the data source is an Atlas serverless instance. MongoDB serverless currently does not support change streams, which are used on watched collections to listen for changes.
Watch for Changes in a Collection
You can open a stream of changes made to a collection by calling collection.watch() or collection.watchAsync(). You can watch for changes to specific documents in a collection by passing the object ids of the objects you would like to monitor as a variable number of arguments.
The following snippet watches for changes to any documents in the
plants
collection:
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully inserted a document with id BsonObjectId{value=5f6bb...} V/EXAMPLE: Event type: INSERT full document: Plant [id=5f6bb..., name=triffid, sunlight=low, color=green, type=perennial, partition=Store 47]
Watch for Changes in a Collection with a Filter
You can open a stream of changes made to documents in a collection that
fulfill certain criteria by calling collection.watchWithFilter() or
collection.watchWithFilterAsync().
Both methods accept a Document
or BsonDocument
parameter that is
used as the query of a $match operator to process each
database event that occurs while watching the
collection.
The following snippet watches for changes to documents in the
plants
collection, but only triggers the provided callback for
events corresponding to documents belonging to the partition named
"Store 42":
Running this snippet produces output resembling the following:
V/EXAMPLE: successfully inserted 2 documents into the collection V/EXAMPLE: Event type: INSERT full document: Plant [id=5f6bb..., name=venomous tentacula, sunlight=low, color=brown, type=annual, partition=Store 42]
Aggregate Documents in a Collection
Aggregation operations run all documents in a collection through a series of data aggregation stages called an aggregation pipeline. Aggregation allows you to filter and transform documents, collect summary data about groups of related documents, and other complex data operations.
You can execute an aggregation pipeline using collection.aggregate().
An aggregation operation accepts a list of aggregation stages as input and returns a Task that resolves to a collection of documents processed by the pipeline.
Filter Documents
You can use the $match stage to filter documents according to a Query API query filter.
{ "$match": { "<Field Name>": <Query Expression>, ... } }
Example
The following $match
stage filters documents to include
only those where the type
field has a value equal to "perennial":
Group Documents
You can use the $group stage to aggregate summary
data for one or more documents. MongoDB groups documents based
on the expression defined in the _id
field value of the $group
stage. You can reference a specific document field by prefixing the
field name with a $
.
The following snippet groups all documents in the plants
collection by their type
value and aggregates a count of the
number of each type:
Project Document Fields
You can use the $project stage to include or omit specific fields from documents. Additionally, you can calculate new fields using aggregation operators. Projections work in two ways:
Explicitly include fields with a value of 1. This has the side-effect of implicitly excluding all unspecified fields.
Implicitly exclude fields with a value of 0. This has the side-effect of implicitly including all unspecified fields.
These two methods of projection are mutually exclusive: if you explicitly include fields, you cannot explicitly exclude fields, and vice versa.
Note
The _id
field is a special case: it is always included in every
query unless explicitly specified otherwise. For this reason, you
can exclude the _id
field with a 0
value while simultaneously
including other fields, like _partition
, with a 1
. Only the
special case of exclusion of the _id
field allows both exclusion
and inclusion in one $project
stage.
{ "$project": { "<Field Name>": <0 | 1 | Expression>, ... } }
Example
The following $project
stage omits the _id
field, includes
the name
field, and creates a new field named storeNumber
.
The storeNumber
is generated using two aggregation operators:
$split
separates the_partition
value into two string segments surrounding the space character. For example, the value "Store 42" split in this way returns an array with two elements: "Store" and "42".$arrayElemAt
selects a specific element from an array based on the second argument. In this case, the value1
selects the second element from the array generated by the$split
operator since arrays index from0
. For example, the value ["Store", "42"] passed to this operation would return a value of "42".
Add Fields to Documents
You can use the $addFields stage to add new fields with calculated values using aggregation operators.
Note
$addFields
is similar to $project but does not allow you to
include or omit fields.
Example
The following $addFields
stage creates a new field named
storeNumber
where the value is the output of two aggregate operators
that transform the value of the _partition
field.
Unwind Array Values
You can use the $unwind stage to transform a single document containing an array into multiple documents containing individual values from that array. When you unwind an array field, MongoDB copies each document once for each element of the array field but replaces the array value with the array element in each copy.
{ $unwind: { path: <Array Field Path>, includeArrayIndex: <string>, preserveNullAndEmptyArrays: <boolean> } }
Example
The following $unwind
stage creates a new document for each
element of the items
array in each document. It also adds a field
called itemIndex
to each new document that specifies the
element's position index in the original array:
Consider the following document from the a collection of purchases:
{ _id: 123, customerId: 24601, items: [ { name: "Baseball", quantity: 5 }, { name: "Baseball Mitt", quantity: 1 }, { name: "Baseball Bat", quantity: 1 }, ] }
If we apply the example $unwind
stage to this document, the stage
outputs the following three documents:
{ _id: 123, customerId: 24601, itemIndex: 0, items: { name: "Baseball", quantity: 5 } }, { _id: 123, customerId: 24601, itemIndex: 1, items: { name: "Baseball Mitt", quantity: 1 } }, { _id: 123, customerId: 24601, itemIndex: 2, items: { name: "Baseball Bat", quantity: 1 } }