Dabbling with MongoDB Query API

Henna Singh
3 min readMar 2, 2022
#BuildTogether Day24/100

Yet another long day….. 😅 I really need to re-think my work strategy 😛. But nothing gives me more pleasure than writing down my nth/100 day =D

So today, I switched gears and did some experiments with MongoDB Query API ;)

MongoDB Realm provides SDKs that help connect mobile apps to the Realm backend. The SDK provides the MongoDB Realm functionality to authenticate users with any of the built-in Authentication providers, call backend functions, and directly access a MongoDB database.

Query API

This can be used to directly access the data in the MongoDB Database from within a mobile application.

MongoDB and Realm are different databases at many levels. If there is Sync enabled on the cluster, then ONLY Sync Rules and Permissions will be used.

Very limited CRUD operations can be performed from within the mobile application. Although the aggregation pipeline feature which is absolutely a spaghetti :P in my head can run from within the application.

If sync is not enabled, then MongoDB role-based rules need to be applied to secure the data retrieval.

Getting Started

No major steps here, I spin an M0 cluster, created a Realm App, linked it with the cluster that will be my data source, and enabled Sync as at the moment I am more familiar with Sync permissions than MongoDB roles (maybe someday).

On the Mobile front, added realm dependencies actually used already existing app :P, linked realm app id, and added an anonymous authentication provider and that was it.

The next step was to instantiate a local MongoDB collection handle.

The code is directly from the docs, I won’t say that I understood the CodecRegistry thing, but on a high level, I inferred its to make MongoDB understand the Plant Java Object Class that I have.

mongo-atlas here is the data-source name. You can get this name from your Linked Data Sources section on MongoDB Cloud.

You can edit the name if you like but I kept the same as it was when the cluster was created and the app was linked to it.

The Plant Class is as below:

Something about @BsonProperty annotation here, something I found it, its for the name of the partition on the server-side, while class name is referenced as partition .

Now, for CRUD operations, Sync Rules will come into play. When I set partition sync on the cloud, I used the following permissions:

I wanted to test if with these permissions, I could create collections or insert data, so I performed the mentioned query

and it turned out I cannot insert data if I dont have correct permissions.

2022-03-02 18:46:55.754 25674-25674/com.geek.mongoapi E/PlantActivity: Failed to insert documents with:
SERVICE_UNKNOWN(realm::app::ServiceError:-1): no rule exists for namespace 'plant-data-database.plant-data-collection'

New Things I learned

  • The built-in MongoCollection does not support the distinct query. So if you wanna find out distinct values of a field, you may not be able to do it from within the app.
  • Realm supports limited set of CRUD commands.
  • Rules are important, I believe I would verify with all set of permissions, Write permission is most likely needed for inserting the data
  • Relationships of any form; One to Many, or Many to Many are a challenge. Realm SDK support “backlinks” while there is no way to represent them in server-side relationships.

Today was a head explosion with all the new things… Will continue this tomorrow as well =)

--

--

Henna Singh

Technical Speaker and an aspiring writer with avid addiction to gadgets, meet-up groups, and paper crafts. Currently pursuing Full Stack Bootcamp from Nucamp