Creating Databases and Collections Correctly

MongoDB's shell makes it easy to select a database and create collections, but database selection and physical creation are not the same operation.

Selecting a database

use learning

This changes the current database context in mongosh. It does not necessarily create a database immediately.

Creating a collection implicitly

A collection can be created when the first document is inserted:

db.courses.insertOne({
 title: "MongoDB Fundamentals",
 level: "beginner"
});

If courses does not exist, MongoDB ordinarily creates it as part of the insert operation.

Creating a collection explicitly

db.createCollection("students");

Explicit creation is useful when you want to configure collection options, validation, or other supported properties before inserting data.

Listing collections

db.getCollectionNames();

The shell returns collection names visible in the current database.

Inspecting collection information

db.getCollectionInfos();

This provides collection metadata available to the connected user.

Naming conventions

Prefer clear, consistent collection names that reflect the records or workload they contain.

Examples include:

  • users
  • courses
  • enrollments
  • orders
  • auditEvents

Naming conventions are not a substitute for data modeling. For example, creating an enrollments collection does not by itself establish referential integrity with a users collection.

Collection validation at creation time

You can combine collection creation with validation:

db.createCollection("students", {
 validator: {
 $jsonSchema: {
 bsonType: "object",
 required: ["name", "email"],
 properties: {
 name: { bsonType: "string" },
 email: { bsonType: "string" },
 age: { bsonType: "int", minimum: 0 }
 }
 }
 },
 validationAction: "error"
});

This example requires name and email to be strings. If age is present, it must be a BSON 32-bit integer greater than or equal to zero. Values represented using other numeric BSON types would not satisfy this specific bsonType rule.

If the collection already exists, the creation command will not simply replace its configuration. Use the appropriate collection-modification command when you need to update existing options.

Create and inspect a practice collection
use mongodb_course_lab

db.createCollection("practiceStudents");

db.practiceStudents.insertMany([
 { name: "Arun", age: 22 },
 { name: "Meera", age: 24 }
]);

printjson(db.getCollectionNames());
printjson(db.getCollectionInfos());
💡 The example explicitly creates a collection, inserts two documents, and inspects collection names and metadata. The collection will remain in the selected database after the shell session ends because the database persists its stored data.
Key Takeaways
  • The use command selects a database context.
  • A database is not necessarily created just because it has been selected.
  • Collections can be created implicitly or explicitly.
  • Explicit collection creation is useful when configuring validation and collection options.
  • Collection names and database structures should follow clear conventions.
Knowledge Check

What does `use learning` do in mongosh?