Mobile Bytes #12: From MongoDB to Mobile - Food Finder App

Howdy Folks,

Today, I am going to share details on how I synced data that is already in MongoDB Atlas to my mobile device and created a restaurant mobile application from it :smiley: Exciting, isn’t it :smiley:

I assume you all are aware how to create an Atlas M0 cluster, but if not please follow MongoDB documentation on creating an account and a free cluster.

Atlas also provides sample data that you can load and use to experiment with MongoDB and use features like App Services and Atlas Device Sync. Load the sample data once you have created the cluster.

The collections can be viewed from “Browse Collections” tab displayed on the cluster in the Atlas UI.

I will be using the Atlas sample_restaurants dataset to create a FoodFinder application.

Atlas App Services provides a way to create an application and link data in Atlas to it. There are multiple templates that you can choose from. I chose to build an app from scratch and added details as shown:

When you click on “Create App Services” this will create a backend application for you. At this moment, the application is not linked with any data or front-end, but shows the dashboard of all features that you can incorporate into your backend

In the next step, you will make this application aware of the restaurant data and that will led us to define an App Services Schema.

Click on the schema tab from the left panel and choose “sample_restaurants.restaurants” from the collections.

There are two ways in which this schema can be generated. You can refer to Mobile Byte : Book Application that creates the schema from mobile and then syncs it to cloud.

For FoodFinder app, choose “Generate a Schema” option. This will further lead you to define the number of documents to generate schema from. I try to put a higher number so that schema is obtained from maximum documents. I added the number 10k and clicked to sample the documents.

It will take a few minutes to generate the schema from the sampling. Save Draft and Deploy once completed.

Schema

When we create a mobile application using Atlas as a backend, the schema is modeled in two formats

  1. App Services Schema: This is also called server-side schema which defines the data in BSON, MongoDB Document format. This is generated by sampling the documents already in an Atlas database, like you did above. This becomes the super-set schema of the restaurant application.
  2. Realm Object Model (Realm SDKs): This is the schema generated for client applications. This can be a subset of the App Services schema depending on the requirements of the application.

Both the schemas are required to be consistent with each other and that’s what enables Atlas Device Sync functionality.

Check Sync Data Model Overview for more information.

Click on Realm SDKs tab on the left and Realm Object Models, you will see schema is created in multiple languages, that you can choose from and paste it in your mobile application.

Check Realm SDK documentation for language specific syntax and details.

The last step is to enable Atlas Device Sync on the application and then an Atlas backend is ready to be linked to a mobile application.

I will not go into details of Atlas Device Sync for this post, please refer to Mobile Byte: Understanding partition and flexible sync and other mobile bytes for understanding Sync.

Please view my talk at London Java Community on how I used flexible sync to sync restaurant data and created mobile application: LJC: From MongoDB to Mobile September 26th, 2022.

I hope you all enjoyed watching and learning about App Services and Realm. Please don’t hesitate to reply to this discussion topic if you have any questions or feedback.

You can register your interest in contributing to the mobile bytes.

Challenge: I have not implemented Reviews Collection and respective business logic in the application. An explanation of how to do this is given in the video. I challenge you to try yourself and share your experiences in the comments for this post.

3 Likes