> For the complete documentation index, see [llms.txt](https://tingreavinash.gitbook.io/the-tech-toolbox/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tingreavinash.gitbook.io/the-tech-toolbox/notes/database/mongodb.md).

# MongoDB

### MongoDB Basics

| Command             | Description                                                        |
| ------------------- | ------------------------------------------------------------------ |
| `mongod`            | Start MongoDB server                                               |
| `mongo`             | Open the MongoDB shell                                             |
| `show dbs`          | Show all the available databases                                   |
| `use db_name`       | Switch to the specified database or create one if it doesn't exist |
| `db`                | Display the current database you are using                         |
| `db.dropDatabase()` | Drop the current database                                          |

### CRUD Operations

#### Insert

| Command                                                 | Description                                   |
| ------------------------------------------------------- | --------------------------------------------- |
| `db.collection.insertOne(document)`                     | Insert a new document into the collection     |
| `db.collection.insertMany([document1, document2, ...])` | Insert multiple documents into the collection |

#### Find

| Command                        | Description                                            |
| ------------------------------ | ------------------------------------------------------ |
| `db.collection.find()`         | Retrieve all documents from the collection             |
| `db.collection.find(query)`    | Retrieve documents that match the specified query      |
| `db.collection.findOne()`      | Retrieve one document from the collection              |
| `db.collection.findOne(query)` | Retrieve one document that matches the specified query |

#### Update

| Command                                        | Description                                       |
| ---------------------------------------------- | ------------------------------------------------- |
| `db.collection.updateOne(query, update)`       | Update the first document that matches the query  |
| `db.collection.updateMany(query, update)`      | Update all documents that match the query         |
| `db.collection.replaceOne(query, replacement)` | Replace the first document that matches the query |

#### Delete

| Command                           | Description                                      |
| --------------------------------- | ------------------------------------------------ |
| `db.collection.deleteOne(query)`  | Delete the first document that matches the query |
| `db.collection.deleteMany(query)` | Delete all documents that match the query        |

### Query Operators

#### Comparison Operators

| Operator | Description                                                        | Example                                |
| -------- | ------------------------------------------------------------------ | -------------------------------------- |
| `$eq`    | Matches values that are equal to a specified value                 | `{ age: { $eq: 18 } }`                 |
| `$ne`    | Matches all values that are not equal to a specified value         | `{ age: { $ne: 18 } }`                 |
| `$gt`    | Matches values that are greater than a specified value             | `{ age: { $gt: 18 } }`                 |
| `$gte`   | Matches values that are greater than or equal to a specified value | `{ age: { $gte: 18 } }`                |
| `$lt`    | Matches values that are less than a specified value                | `{ age: { $lt: 18 } }`                 |
| `$lte`   | Matches values that are less than or equal to a specified value    | `{ age: { $lte: 18 } }`                |
| `$in`    | Matches any of the values specified in an array                    | `{ name: { $in: ['Alice', 'Bob'] } }`  |
| `$nin`   | Matches none of the values specified in an array                   | `{ name: { $nin: ['Alice', 'Bob'] } }` |

#### Logical Operators

| Operator | Description                              | Example                                            |
| -------- | ---------------------------------------- | -------------------------------------------------- |
| `$and`   | Joins query clauses with a logical AND   | `{ $and: [ { name: 'Alice' }, { age: 18 } ] }`     |
| `$or`    | Joins query clauses with a logical OR    | `{ $or: [ { name: 'Alice' }, { name: 'Bob' } ] }`  |
| `$not`   | Inverts the effect of a query expression | `{ age: { $not: { $gte: 18 } } }`                  |
| `$nor`   | Joins query clauses with a logical NOR   | `{ $nor: [ { name: 'Alice' }, { name: 'Bob' } ] }` |

#### Element Operators

| Operator  | Description                                     | Example                      |
| --------- | ----------------------------------------------- | ---------------------------- |
| `$exists` | Matches documents that have the specified field | `{ age: { $exists: true } }` |

#### Create collection in MongoDB

To create a collection in MongoDB, you can use the `db.createCollection()` method in the MongoDB shell. The syntax for creating a collection is as follows:

```
db.createCollection("<collection_name>", <options>)
```

Where `<collection_name>` is the name of the collection you want to create and `<options>` are the optional parameters that you can use to configure the collection. Here are some examples of how you can create a collection:

1. Create a collection without any options:

```
> db.createCollection("users")
```

This will create a collection named "users" in the current database.

2. Create a collection with options:

```
> db.createCollection("users", {
    capped: true,
    size: 100000,
    max: 100
  })
```

This will create a capped collection named "users" with a maximum size of 100,000 bytes and a maximum number of documents of 100.

You can also create a collection using the `MongoCollection` class in a MongoDB driver for your programming language. The specific syntax for doing this will depend on the driver you are using.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://tingreavinash.gitbook.io/the-tech-toolbox/notes/database/mongodb.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
