/

The Basics of MongoDB: A Tutorial

The Basics of MongoDB: A Tutorial

MongoDB is a database system that is responsible for storing and retrieving information in an application. As a NoSQL database, MongoDB does not use the SQL language for querying data.

Key Characteristics of MongoDB

  • MongoDB is a JavaScript-friendly database, providing a JavaScript API for creating databases and collections of objects called “documents”.
  • It is schemaless, meaning that there is no need to pre-define a structure for the data before storing it.
  • In MongoDB, any object can be stored without worrying about its fields or how they should be stored. You simply tell MongoDB to store the object.
  • Data in MongoDB is stored in a format similar to JSON but allows for storing more than just basic data types.

Installation

To install MongoDB on a Mac, follow these steps:

  1. Open the terminal.
  2. Run the following commands:
1
2
brew tap mongodb/brew
brew install mongodb-community

That’s it! These instructions assume you are familiar with using the terminal and have already installed Homebrew.

After installation, MongoDB can be launched in two ways:

  1. Start MongoDB now and restart it every time you log in:
1
brew services start mongodb-community
  1. Run MongoDB only when needed:
1
mongod --config /usr/local/etc/mongod.conf

The default configuration for MongoDB includes storing logs in /usr/local/var/log/mongodb/mongo.log and the database in /usr/local/var/mongodb. By default, there is no access control, allowing anyone to read and write to the database.

Interacting with MongoDB: The Mongo Shell

MongoDB can be interacted with using the mongo program, which opens the MongoDB shell. This shell allows you to enter commands that MongoDB understands.

Creating a Database

By default, MongoDB creates a database called “test”. You can verify the active database by running db in the shell. To switch to a different database, use the command use newname, which creates the new database and switches the shell to use it.

To view the available databases, use the command show databases. Note that a database will only be listed if it contains at least one collection. Creating a collection is similar to creating a SQL database table. Use the db.createCollection() command with the database name as the first argument. This will list the new database when show databases is run and the new collection when show collections is run.

You can also create a new collection by using it as a property of the db object and calling the insert() method to add an object to the collection. For example:

1
db.dogs.insert({ name: 'Roger' })

To view the objects in a collection, use the find() method. Additional fields like _id are automatically generated by MongoDB. You can add more objects to a collection using the insert() method and pass parameters to filter and retrieve specific entries. The find() method returns a cursor that must be iterated on. Alternatively, you can use the findOne() method to retrieve only the first record if multiple records match a query.

Updating and Removing Records

To update a record, use the update() method on a collection. To remove a record, use the remove() method and pass an object to help identify the record. To remove all entries from a collection, pass an empty object to the remove() method.

Tags: MongoDB, NoSQL, JavaScript API, Schemaless, Database, Collections, Installation, Mongo Shell, Create Database, Collections, Find, Update, Remove