A Complete Beginners Guide to MongoDB

Getting Started

See our article HERE to install and use of MongoDB on Ubuntu

MongoDB is a open-source NoSQL database that is designed for scalability, high performance, and ease of development. It is based on a document-oriented model, which means that data is stored in the form of JSON format called BSON. This data model allows you to store complex data structures and relationships in a more flexible way than traditional relational databases, and it is well-suited to modern, data-driven applications that require real-time access to large volumes of data.

MongoDB is known for its easy-to-use query language and rich indexing options. It also has a strong developer community and a wide range of tools and integrations available to support its use in a variety of applications.

Create Database

To create a new database in MongoDB, you can use the "use" command followed by the name of the database you want to create. If a database with the specified name does not exist, MongoDB will create it for you.

Here's an example of how to create a new database called "mydb" in the MongoDB shell:

use mydb

Show All Databases

To show all databases in MongoDB, you can use the "show dbs" command in the MongoDB shell. This will list all databases on the MongoDB server, along with their sizes.

Here's an example of how to use the "show dbs" command:

show dbs

This command will show you all the databases on the MongoDB server, including the system databases

Create Collections

There are 2 ways to create a collection.

Method 1

You can create a collection using the "createCollection()" database method.

For example:

use mydb
db.createCollection("customers")

This will create an empty "customers" collection in the current database.

Method 2

You can insert a document into the collection. If the collection does not exist, MongoDB will create it automatically.

db.customers.insert({ name: "Stan Stev" })

This will create the "customers" collection and insert a document with the name "John Smith" into the collection.

Show All Collections

To show all the collections in a MongoDB database, you can use the "show collections" command in the MongoDB shell.

Alternatively, you can also use the "db.getCollectionNames()" method in a MongoDB driver for your programming language to retrieve a list of all the collections in a database. This method is part of the MongoDB driver and can be used to get a list of collections in a database from a MongoDB client object that is connected to that database.

use mydb
show collections
OR
db.getCollectionNames()
                    

This will display a list of all the collections in the "mydb" database.

Insert Documents

The "insertOne" and "insertMany" methods in MongoDB are used to insert one or multiple documents, respectively, into a collection. These methods are similar to the insert method, but they offer more control over the write operation and provide additional options for handling errors and specifying write concerns.

Here's an example of how to use the "insertOne" method to insert a single document into a collection called "customers":

db.customers.insertOne({ name: "Stan Stev" })

You can use the insertMany method to insert multiple documents at the same time. For example:

db.customers.insertMany([
    { name: "Jane Smith" },
    { name: "Bob Smith" }
])

Find Data

To find data in a collection in MongoDB, you can use the "find" method on the collection.

Here's an example of how to use the "find" method to find all documents in a collection called "customers":

db.customers.find()

This will find all documents in the "customers" collection and print them to the console.

You can use the "find" method with a query parameter to filter the documents.

db.customers.find({ name: "Stan Stev" })

This will find all documents in the "customers" collection with the field "name" set to "Stan Stev" and return them.

You can also use the "findOne" method to find the first document in a collection that matches a query. For example:

db.customers.findOne({ name: "Stan Stev" })

This will find the first document in the "customers" collection with the field "name" set to "Stan Stev" and return it.

Update Document

To update a document in a collection in MongoDB, you can use the "updateOne" or "updateMany" method on the collection.

Here's an example of how to use the "updateOne" method to update a document in a collection called "customers":

db.customers.updateOne({ name: "Stan Stev" }, { $set: { age: 30 } })

This will update the first document in the "customers" collection with the field "name" set to "Stan Stev", and set the "age" field to 30.

You can use the "updateMany" method to update all documents that match a filter. For example:

db.customers.updateMany({ name: /^Stan/ }, { $set: { age: 30 } })

This will update all documents in the "customers" collection with a "name" field that starts with "Stan", and set the "age" field to 30.

If you set the "upsert" option to "true", a new document will be inserted if no documents match the filter. If you set the "upsert" option to "false", no documents will be inserted.

db.customers.updateOne({ name: "Stan Stev" }, { $set: { age: 30 } }, { upsert: true })

Delete Documents

To delete a document in a collection in MongoDB, you can use the "deleteOne" or "deleteMany" method on the collection.

Here's an example of how to use the "deleteOne" method to update a document in a collection called "customers":

db.customers.deleteOne({ name: "Stan Stev" })

This will delete the first document in the "customers" collection with the field "name" set to "Stan Stev".

You can use the "deleteMany" method to delete all documents that match a filter. For example:

db.customers.deleteMany({ age: { $lt: 30 } })

This will delete all documents in the "customers" collection where the field age is less than 30.

MongoDB Query Operators

MongoDB provides a number of query operators that you can use to filter and manipulate the data in a collection. Here is a list of some of the most commonly used query operators

  • $eq: Matches documents where the value of a field is equal to the specified value.
  • $gt, $gte: Matches documents where the value of a field is greater than ($gt) or greater than or equal to ($gte) the specified value.
  • $lt, $lte: Matches documents where the value of a field is less than ($lt) or less than or equal to ($lte) the specified value.
  • $ne: Matches documents where the value of a field is not equal to the specified value.
  • $in: Matches documents where the value of a field is in the specified array.
  • $nin: Matches documents where the value of a field is not in the specified array.
  • $or: Matches documents where any of the conditions specified in the array are met.
  • $and: Matches documents where all of the conditions specified in the array are met.
  • $not: Matches documents where the condition specified is not met.
  • $exists: Matches documents where the field specified exists (true) or does not exist (false).

Here is an example of how to use some of these query operators in a MongoDB query:

db.collection.find({
    $or: [
        { age: { $gt: 30 } },
        { name: { $in: ["Stan", "Stev"] } }
    ]
})

This query will return all documents in the collection where the field age is greater than 30 or the field name is "Stan" or "Stev".