We created a simple REST API that integrates with a MySQL database in my previous article. We will do the same in this article. But with MongoDB as a NoSQL database.
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database. MongoDB uses JSON-like documents with optional schemas.
NoSQL refers to any non-relational database. NoSQL databases can store relationship data. They just store it differently than relational databases do. NoSQL data models allow related data to be nested within a single data structure.
SQL databases use structured query language and have a predefined schema. NoSQL databases have dynamic schemas for unstructured data. SQL databases are table-based, while NoSQL databases are document, key-value, graph, or wide-column stores.
It has a learning curve while working with relational databases because we need to understand SQL. It requires a schema while working with non-relational databases and a client library to integrate it with the programming language. Most NoSQL database systems perform fast, are flexible, are best for unstructured data.
We created the same CRUD functions as we did to the version of MySQL. The difference is these are for MongoDB. There is no SQL syntax here.
We use bson.D{} as a document to find, filter, and update. We use primitive.NewObjectID() to create a new ID and primitive.ObjectIDFromHex() to read the ID.