MongoDB notes
Installation
Installation guide for ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04
Installation on macos: brew install mongodb-community
MongoDB compass is the free UI that connects to MongoDB: https://www.mongodb.com/docs/compass/current/install/
Installing only the Mongo shell (mongosh
): https://www.mongodb.com/try/download/shell
Why MongoDB?
MongoDB employs a document-oriented data model, where data is stored in BSON (Binary JSON) documents. These documents are collections of key-value pairs and support nested structures, allowing for the representation of complex data hierarchies. This inherent flexibility makes MongoDB suitable for projects with evolving data schemas and diverse data sources (which is key for a project like Drone Data Pilot)
MongoDB version/options
There are two versions of MongoDB: community and enterprise.
Pricing for the enterprise version is not mentioned. We need to reach out to the sales team to find it out.
Advantages of the Enterprise version:
MongoDB Management Service (backup and monitoring solution)
SNMP monitoring
Kerberos or LDAP as an alternative to password-based or certificate-based authentication
Support and training contract
Encrypted Storage Engine to (optionally) protect data at rest
It doesn’t seem worth it to pay for the enterprise version for these features since we aren’t going to allow access to everyone based on LDAP. If the number of users accessing the database directly is limited, community edition makes the most sense.
Enabling authentication
It is important to note that the default mongoDB behavior is having no authentication. Hence, we need add users to restrict access.
Connect to MongoDB server using mongo shell (
mongosh
) instance (if not localhost):mongo mongodb://<host>:<port>
The port number will likely be
27017
, but for additional security, you can always change it to a different one.
use admin
to change to the admin databaseUse the code below to create a user (in this example the user has full admin privileges and can create and delete users as well)
db.createUser(
{
user: "useradmin",
pwd: "thepianohasbeendrinking",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Available built-in roles: https://www.mongodb.com/docs/manual/reference/built-in-roles/#database-user-roles
Dev notes
Instead of tables, a MongoDB database stores its data in collections.
You do not need to specify a schema for a collection in MongoDB. Although it is common for the documents in a collection to have a largely homogeneous structure, it is not a requirement; i.e. documents in a single collection do not need to have the same set of fields. The data type for a field can differ across documents in a collection as well.
To change the structure of the documents in a collection, update the documents to the new structure. For instance, add new fields, remove existing ones, or update the value of a field to a new type.
you can enforce document validation rules for a collection during update and insert operations.