Plugging into MongoDB

DatabasesBackend Development
Michael  Solati
Michael Solati
Jan 17, 2023
Plugging into MongoDBPlugging into MongoDB

Rapid backend development is one reason Amplication is so compelling when building your next project. Instead of worrying about if your endpoints are secure, implementing an authentication layer, or building relations between entities in your database, Amplication handles all of these concerns for you. There is a beauty and simplicity in what Amplication provides Node developers; that same approach can, and should, also apply to the databases you manage. While we've been opinionated in the sort of database applications Amplication generates use, we started to open up the world of possibilities with version 1.0.0 when we introduced plugin support. With a plugin, we allowed projects to switch from PostgreSQL to MySQL, and now we've introduced support for MongoDB.

What is MongoDB?

MongoDB is the most popular NoSQL database that follows a document-oriented structure (think JSON). Being document-oriented, rather than table-focused, allows for a schemaless database. This data model enables MongoDB to store and flexibly manage data compared to traditional relational databases. For example, rather than worrying about different columns in a table when changing the type of a field, MongoDB allows for easy creation, updating, and deleting fields from a data structure without changing the underlying database schema.

At the beginning of a new project, when you may be figuring out your needs and the data model you're using, MongoDB can shine! MongoDB lets developers redefine what the data looks like whenever they need to, without structuring their data in advance. This is another win for rapid backend development, something we constantly try to make easier for developers at Amplication.

How do I get started with MongoDB?

So you're ready to use MongoDB and don't know how to get started; that's ok because switching over is as simple as clicking a button. We'll start with creating a new project and a new service for the project; you can do this at app.amplication.com.



With your service created, you may want to set up GitHub sync; it'll be really useful when we migrate from PostgreSQL to MongoDB so we can see the git diff. With GitHub sync enabled, be sure to rebuild your app or commit your changes so that GitHub will have the latest code from Amplication.



Now is when we can swap databases. Inside the service's UI, click "Plugins" in the left-side menu. You'll see a list of available plugins, including the MongoDB plugin, which we can enable by clicking "Install." Then, commit your changes and jump to the generated GitHub PR to see the differences.



Most of the changes made are to Dockerfiles, which spin up the local MongoDB instance for local development. The most critical file change is the schema.prisma file, where the datasource updates from postgresql to mongodb. The schema.prisma file is where we tell the Prisma client abstracts all database calls and what database source to use.

What else does MongoDB do?

MongoDB is known for its high performance and fast access to data. MongoDB uses various techniques, such as indexing and sharding, to optimize data access and ensure that queries are quick and efficient. MongoDB handles indexing and querying data through its use of BSON. BSON, Binary JSON, is how MongoDB stores data internally and extends the capabilities of regular JSON while also encoding data in a binary structure. BSON encodes information such as the type and length of information. By encoding metadata about the fields, MongoDB can quickly jump to specific data fields to modify or read data with reading the entire document.

{
	"hello": "world"
}

Translates to:

\x16\x00\x00\x00 // total document size
\x02 // 0x02 = type String
hello\x00 // field name
\x06\x00\x00\x00world\x00 // field value
\x00 // 0x00 = type EOO ('end of object')

With its encoding methods, the BSON data format, and other design choices, MongoDB is highly scalable in terms of the amount of data it can store and the number of requests it can support. Using a distributed database system, MongoDB can "shard" its data; distribute it across multiple servers. Developers can also easily add more servers to support growing data volumes and user loads.

Finally, the open-source nature of MongoDB makes it an attractive technology for developers and enterprise companies. A robust, feature-rich database that is free to use and doesn't require a license can be very compelling. The open-source nature of MongoDB is indirectly one of the reasons Amplication was able to develop the MongoDB plugin quickly.

Wrapping up

If you're ready to try out Amplication, we offer great tutorials on building your first application with React or Angular. Just be sure to enable MongoDB when you build your backend.

When you realize that you love Amplication, check out everything we can do for you and your team!