Creating Databases
In contrast to conventional relational database management systems (RDBMS), MongoDB’s flexible and dynamic schema architecture is highlighted by the way databases and collections are generated.
Understanding MongoDB’s Data Model
Data is arranged hierarchically in MongoDB, which is fundamentally a document-oriented database:
- Database: A database is a tangible container for collections that houses a unique collection of files on the file system. It is possible to host numerous databases on a single MongoDB server. An RDBMS considers this a database.
- Collection: A MongoDB document group like an RDBMS table. Collections keep comparable or related documents in a single database, but they do not require a schema, therefore documents might have varied attributes and structures.
- Document: A table of key-value pairs, similar to an RDBMS row or object. The dynamic architecture of documents allows them to have different fields and data types for common fields within a collection. Each document has a unique _id field.
Implicit Creation of Databases and Collections
The implicit or “create on demand” construction process for databases and collections is one of MongoDB’s unique characteristics. A more flexible development process is made possible with MongoDB as opposed to RDBMS, which requires specified tables and schemas.
Implicit Database Creation: Until the first document is written to any collection in a database, the database is not physically created on disc. The MongoDB shell command change the database context by setting the db global variable. On the initial document insertion, this command “switch to” a non-existent database and construct it.
Here’s an example:
- Verify the databases that are currently in place:
- A newly established database (like mydb) won’t display in the list. Test is the default database for collections if none are established.
- Change to a new database, or create one if one doesn’t already exist:
- Put a document into a mynewdatabase collection:
- In the event that neither the movies collection nor the mynewdatabase database already existed, this insert operation will construct them implicitly.
- Check the creation of the database:
- In the list of databases, mynewdatabase is now displayed.
Implicit Collection Creation: In a similar vein, a collection is generated implicitly as soon as the first document is added. Before entering the document, MongoDB will construct the collection name if you attempt to insert into one that doesn’t exist.
Unless otherwise specified, a collection’s documents are assigned a unique _id field. Its 12-byte hexadecimal _id contains a timestamp, machine ID, process ID, and incremental value for global uniqueness. A unique _id index is automatically established for each collection. Special system stores this _id index.Each database implicitly creates an indexes collection.
Explicit Creation of Collections using
There are some situations in which using the db.createCollection() method to directly new a collection is advantageous or required, even when implicit creation provides flexibility:
- Capped Collections: These collections have a set size and act as circular buffers, erasing the oldest documents when the size limit is reached. Because insertion order is maintained by default, capped collections are perfect for logging or caching data where you only need to keep the most recent entries. They have to be made specifically.
- Preallocation of Space: A collection’s space might be preallocated for applications that are performance-sensitive. As MongoDB’s collection expands, this can prevent dynamic file allocation delays.
- Setting specific properties: db.createCollection() lets you set max (maximum number of documents in a capped collection) and autoIndexID (automatically new an index on the _id field for capped collections created before MongoDB 2.2, now default).
The following is db.createCollection()’s fundamental syntax: createCollection() in db.name, options
- name: A string that contains the collection’s name that has to be made. Restrictions on collection names include avoiding using the null character or beginning with system. The collection name database.collection, which is fully qualified, can only be 120–128 bytes.
- options: A document with optional configuration parameters.
Examples of Explicit Collection Creation:
- Putting together a selection with no choices:
- MyexplicitCollection is created in testdb as a result. System.indexes is automatically present even though it is specifically created.
- Putting together a capped collection with choices:
- This example generates a 6,142,800-byte, 10,000-document capped collection named mycappedcollection. MongoDB will delete older documents if size or max are reached first.
Keep in mind that db.collection.insert() and db.collection.createIndex() create new collections. Therefore, explicit creation is not required for a collection’s existence, but it does give additional control over certain aspects.
For completeness, drop databases and collections. The db.dropDatabase() statement deletes the database and its collections. A collection is dropped from the database using db.collection.drop(). Both operations produce success (true or false).