Docs Menu

Docs HomeAtlas App Services

Query Atlas from Client Apps

On this page

  • When to Use MongoDB Data Access
  • Set Up
  • Create an App Services App
  • Add an Authentication Provider
  • Add Rules to a Collection
  • Add a Schema and Filters to Collection (Optional)
  • Query from Client Apps

You can query data in linked MongoDB Atlas clusters from client applications using standard MongoDB query language (MQL) syntax with MongoDB Data Access in the Realm SDKs.

MongoDB Data Access secures data by enforcing your App's role-based access permissions, query filters, and document schemas. These features let you construct complex client-side queries with MQL while your data remains secure on the server.

You might want to use MongoDB Data Access in your application if:

  • You want to use an API like the MongoDB drivers

  • You want to write database queries in your client code instead of on a server

  • You want to construct ad hoc queries from the client device.

You might want to use MongoDB Data Access instead of Atlas Device Sync in an application in these scenarios:

  • You are querying documents that are not or cannot be modeled in Realm Database.

  • You want to access collections that are not synced.

  • You do not want to persist data locally.

  • You are using the Realm Web SDK, which does not support Realm Database or Device Sync.

As an alternative to MongoDB Data Access in client-side scenarios, you can use the Atlas Data API.

You can also use these APIs without a Realm SDK for your client app's programming language as long as the language has a HTTPS client. However, these APIs do not feature the same type-safe MQL syntax as the Realm SDKs' MongoDB Data Access.

As alternative to MongoDB Data Access in server-side scenarios, you can use the following:

  • MongoDB driver. Using a MongoDB driver to connect to Atlas is the generally recommended server-side approach, as the drivers are more performant and flexible than connecting through App Services. If you would like to apply App Services Rules to driver operations, you can use the App Services Wire Protocol.

  • Atlas Data API

To use MongoDB Data Access from a Realm SDK, you must perform the following set up.

1

Create an App Services App with a linked MongoDB data source.

To learn more about App creation, refer to Create an App and Link a Data Source.

2

Add at least one authentication provider to the App. All MongoDB Data Access queries run in the context of an authenticated user. To learn more about enabling user authentication, refer to Authentication Providers.

3

To secure your client-side queries, you must add App Services Rules to any collections that you use with MongoDB Data Access. To learn how to add rules to collections, refer to Define Roles & Permissions.

If you do not add rules to a collection, then every request to it from the client will fail.

4

In addition to rules, you can also add a schema and filters to collections that you query with MongoDB Data Access.

Schemas define your App's data model and validate documents against it. Use filters to ensure that the clients only write data matching the schema to the database.

Filters modify MongoDB queries before they are sent to the database so that queries only return a subset of results. For example, you could define a filter that modifies queries so that a user can only access data that they've written.

While schemas and filters are not necessary to use MongoDB Data Access, you should add them to most production use cases.

5

For documentation on how to query MongoDB from the Realm SDKs, refer to the Realm MongoDB Data Access documentation:

MongoDB Data Access is not supported in these Realm SDKs:

  • C++ SDK

  • Flutter SDK

  • Kotlin SDK

← CRUD & Aggregation APIs