How to Build a MongoDB CRUD App in 4 StepsBy: Ronan McQuillanAug 22, 2022 | 11 minutes readNoSQL databases such as MongoDB seem intimidating at first. The MongoDB CRUD operations come from a different paradigm, which might be confusing if you are used to SQL databases.But it doesnât need to be this way.Instead of manually figuring out how to perform CRUD operations in MongoDB, you can use the power of low-code to do it for you.That is, create an app that handles these differences, allowing you to interact with your DB, just like you would with other tools, like phpMyAdmin.Today, weâre going to look at how to create a MongoDB CRUD app in 4 steps. This will allow you to interact with your DB with no coding knowledge, while still leveraging the full power of NoSQL.Along the way, weâll also explore the concepts and differences between SQL and NoSQL CRUD.Letâs get started!How does MongoDB store data?Instead of tables and rows, MongoDB databases use collections and documents. Collections are similar to tables. They aggregate multiple documents. Documents are the key/value pairs, like row/column pairs in a table.The main difference is that each document can have its own structure. Unlike tables that have fixed columns, MongoDB allows you to store unstructured data. Thus, you can have documents with completely different key/value pairs in the same collection.What is CRUD in MongoDB?CRUD is an abbreviation of Create, Read, Update, Delete. These are the basic operations to interact with databases.The CRUD operations are the same for SQL and NoSQL databases. However, because the data is structured differently, the way we perform CRUD operations is also different.Does NoSQL use CRUD?Yes, just like SQL databases. The main difference is in how these methods are created, and what they do under the hood.In SQL, CRUD operations are SQL commands. In NoSQL, they are commands as well, but in other languages. For instance, MongoDB CRUD commands look more like JavaScript functions with JSON code.Create a MongoDB CRUD app in 4 stepsLetâs see how you can perform operations in MongoDB with a simple CRUD app . Today, we have an example of an app to manage your sales. For simplicityâs sake, weâve got an existing data model, with the following attributes, as well as a few others that we donât need:id.date.location.customer.On the main page youâll see the first operation, read. You can read data from a collection, and filter it in case you are looking for a specific document.Then, if you click on the âeditâ button, youâll see the next two CRUD operations: update and delete. On this page, you can edit your documents. If you want, you can delete them too.And lastly, the âCreateâ operation. You can do it from the home screen if you click on âAdd newâ. This screen is just like the edit screen. The only difference is that you wonât have any pre-populated data.You can create this app in 4 simple steps:Create the app, and connect to MongoDB.Set up the MongoDB CRUD operations.Create the home screen.Create the add new/edit screen.How to create a MongoDB CRUD app in BudibaseToday, weâre going to cover exactly how you can build a MongoDB CRUD app using Budibase, the leading open-source, low-code platform .Letâs just jump straight in.Step 1 - Create the app and connect to MongoDBIf you havenât already, go ahead and create your Budibase and MongoDB accounts.Then, from the main Budibase screen, create a new app and select MongoDB as the data source .Simply add your connection name, connection string, and DB name.If you donât have a connection string, you can find it by clicking on the âConnectâ button, next to your cluster name:Any of these methods will give you a connection string. For this example, I just picked the connection string from the MongoDB Compass option.Thatâs it! Now Budibase is connected to your MongoDB instance. Connecting Budibase to external data sources really couldnât be much easier.Step 2 - Set up the MongoDB CRUD operationsIn this step, youâll create the CRUD operations to manipulate your database. Still in the âDataâ Panel of Budibase with your MongoDB connection selected, click âAdd queryâ.Youâll need four queries:Get_sales - Read the sales data, and filter based on the properties.Update_sale - Update operations, to edit entries.Insert_sale - Add new documents.Delete_sale - Delete a document.The get_sales query uses these settings:Our sample data includes some properties that we wonât use. Itâs likely that in your real application you might need to hide some data as well. Remember NoSQL data is unstructured, so there might be a whole raft of different variables in your data model.You can remove these from your query using this transformer:return data.map( ( { _id, saleDate, storeLocation, customer } ) => ({ "ID": _id, "Date": saleDate, "Location": storeLocation, "Customer": customer.email }) ) This removes all properties except the id, date, location, and customer email.You can run your query to make sure it works as you wanted. Then save it.The insert_sale query looks like this:We are using some bindings there. They allow you to fill in forms and send this data to your DB. In the fields option, use this:{ "saleDate": "{{Date}}", "customer": { "email": "{{Customer}}" }, "storeLocation": "{{Location}}" } These are the settings we need to tell MongoDB how to store the form data.Add a new query, and use these settings for the update_sale operation:Notice how this one is similar to the insert query.The only difference is that we need to get the id, so we know which item to update. And these are the fields to do it:{ "_id": "ObjectID('{{ID}}')" }, { "$set": { "saleDate": "{{Date}}", "customer.email": "{{Customer}}", "storeLocation": "{{Location}}" } } The first portion is the âsearchâ part. The second part is the update instruction.If there are multiple documents that match the search criteria, only the first one is updated. If you want to update multiple documents at once, you can use the âupdateManyâ action type (as opposed to âupdateOneâ).The update instructions include our bindings as variable values. They get data from our form fields and pass them to MongoDB.Run the query and save it.Lastly, add a new query for the delete_sale operation.Be very careful with this operation. If possible, itâs better to move records to an archive collection or add an âarchiveâ property to them, as opposed to deleting them.You can set up your query like this:Make sure to include your fields to define which items you want to delete:{ "_id": "ObjectID('{{ID}}')" } Now run it, and save the query.In addition to the basic operations, you can run many complex filters and use other options. You can read the MongoDB docs for detailed instructions on all the possibilities.Thatâs all you need for the database connection. Letâs create the visual elements of your app so that they reflect our database schema .Step 3 - Create the home screenThe home screen features a button to add new records, a table to view sales, and a filter to get the exact items you want to see.You can create and edit screens in the âdesignâ tab.Before creating your screen, like any good app, we need a dark theme. Only joking, you can use whichever theme you like.You can style your Budibase app under the themes menu:Pick a theme and your preferred accent color. Then head over to the âscreensâ section. If you havenât already, create a new screen, and select it as your appâs home page:Go to the âcomponentsâ section and letâs start building your page. Add a button for the âAdd newâ option, and set the âonclickâ action to navigate to /home/0.This may seem weird, but we are going to use the /home/:id path to edit entries. Therefore, to create a new item, we just need an invalid ID. With an invalid ID (zero), your edit form is removed, and the âadd newâ form appears.In Budibase, whenever you want to load data, you can add a data provider as the source and an iterator to display it. You can create a data provider and set it with the get_sales provider:Then, add a dynamic filter and a table inside of it, like this:Lastly, add the âeditâ button to each of your rows. You can do this by clicking on the table, then on add new component, and selecting the button component. This will add a button inside your table.Set the âonclickâ action to navigate to URL. The path is this binding:/home/{{ Sales.get_sales.ID }} Here you are saying to Budibase âuse the âSalesâ table and the âget_sales.IDâ valueâ. This means that, if the row id is âabc100â, the target URL is â/home/abc100â.And thatâs all you need for the âreadâ portion of your MongoDB CRUD app. Itâs time to create the edit form UI .Step 4 - Create the add new/edit screenCreate a new screen, and in the route, put /home/:id. This route is going to load anything you use in your path after â/home/â as a variable.This option comes in handy since it allows us to use this ID information to either load the document that you want to edit or load the add new form.To make it easier to understand, this is the overall page structure:BreadcrumbsAdd new formTitleForm fieldsButtonData provider (get_sales using the id)RepeaterEdit formTitleForm fieldsButtonNotice that we have 2 forms on a single page. The trick to showing just one of them is to use the âConfigure Conditionsâ option and hide/show them based on the :id value from our URL.So we hide the âNew Formâ if {{ URL.id }} if NOT equal to 0. And we hide the âEdit formâ if the {{ URL.id }} IS EQUAL to 0.The form setup itself is almost the same, so letâs see how you can create the edit form. Itâs a bit more complex.Add a Data Provider. Youâll need this to pre-populate the form fields. Use the get_sales query, and under the filter options, use ID is equal to binding {{ URL.id }}.Then you need an iterator. In the previous screen, you used a table, which is just a way to load all data at once. This time, we are loading items individually, so it makes sense to use the repeater.Inside the repeater, we have a title to show users what is happening. In it, you can load the sale ID with this binding:Edit Sale {{ Repeater.get_sales.ID }} Next, there is a form, which is required to manipulate the form fields. You can use the âUpdateâ type and update_sale as the schema.In the form, you can add a field group. They are very useful since you can add fields for your entire schema by clicking the âUpdate Form Fieldsâ button:This will create 4 fields automatically, ID, Date, Customer, and Location.If you want the date to be a date picker you can delete the default element and add a new date picker component. Just make sure to change the field to Date, so Budibase knows that this is the source of the date information.And while you are at it, use the bindings to load the current value for the date:{{ Repeater.get_sales.Date }} You can repeat this process to load default values for the other fields, using these bindings:{{ Repeater.get_sales.Customer }} {{ Repeater.get_sales.Location }} Feel free to delete the ID field. We are loading the ID from the URL directly, so you donât need it there.Now you need a button to save the form edits. Add a new button, and for the âOnclickâ actions, select âexecute queryâ, using the field values to populate the query bindings:This ensures that the data is saved, you can add another action to navigate to the /home/ page, so you are ready to edit other entries.You could add more options, such as an app state to display confirmation modals. Free free to improve the UI as much as you like and tailor it to your specific needs.The âDeleteâ operation of your CRUD MongoDB app is quite simple. Itâs just one button.Add the delete button, and make sure it is inside the sales repeater. Preferably far away from the update button to avoid any mishaps.Then, use the âonclickâ action for this button, just like you did for the form button. Here youâll run the delete_sale query. Make sure to pass the URL.id to the ID binding.For the âadd newâ form, you can simply copy the entire update form component. Paste it into a new container (outside the sales data provider). Then, remove the default values, and use the insert_sale instead of the update_sale query.If you want, you can add the breadcrumbs as a finishing touch. In it, you can use a container and switch from horizontal stacking to vertical stacking:Then, in it add a link to home, which is fixed, and a paragraph for the > divisor, which is fixed as well. The last paragraph is going to change between âAdd newâ and âEdit saleâ. You can do so by using this JS function:Building a MongoDB CRUD app in 4 stepsToday we looked into how you can create a MongoDB app with Budibase. With it, you can interact with your NoSQL database easily, with no coding knowledge.We offer support for an unrivaled variety of data sources, as well as our built-in database. To see more of what Budibase is capable of, why not check out our range of free, deployable app templates ?We hope youâve enjoyed it. See you again next time!