> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/ask/flat/chapter-35-database-management/di-35.5-jie-mongodb.md).

# 35.5 MongoDB

MongoDB is a NoSQL document database that uses BSON format for storage, featuring dynamic schemas and horizontal scaling capabilities. This section covers the pkg installation, startup configuration, and basic operations of MongoDB 8.0 on FreeBSD.

> **Note**:
>
> Since October 16, 2018, the license for MongoDB Community Server has changed from AGPL v3 to SSPL (Server Side Public License). SSPL is modified from GPL v3 and requires third parties offering MongoDB as a service to open-source their entire service stack source code. SSPL has not been approved by the OSI (Open Source Initiative) as an open-source license, and MongoDB withdrew its OSI approval application in March 2019. Strictly speaking, MongoDB is "source-available" software, not open-source software as defined by the OSI. For specific permitted and prohibited uses under SSPL, please refer to the official MongoDB SSPL FAQ: <https://www.mongodb.com/legal/licensing/server-side-public-license/faq>.

## Installation

Install MongoDB using pkg:

```sh
# pkg install mongodb80
```

Or install using Ports:

```sh
# cd /usr/ports/databases/mongodb80/
# make install clean
```

View the post-installation information for MongoDB:

```sh
# pkg info -D mongodb80
```

## Service

After installation is complete, start the MongoDB service:

```sh
# service mongod enable   # Set the MongoDB service to start on boot
# service mongod start    # Start the MongoDB service
```

## mongosh (MongoDB Official Shell CLI)

MongoDB 6.0 and later no longer include the legacy `mongo` command-line tool; it has been replaced by the official `mongosh`. The `mongosh` package is available in the FreeBSD Ports collection and requires no additional steps.

### Installing mongosh

Install using pkg:

```sh
# pkg install mongosh
```

Or install using Ports:

```sh
# cd /usr/ports/databases/mongosh/
# make install clean
```

### Testing the MongoDB Connection

Use `mongosh` to connect to the local MongoDB service:

```sh
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bca91ccf5bbd1a232d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2025-02-25T01:08:25.311+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test>
```

## Configuration File

Related project file structure:

```sh
/
└── usr/
    ├── local/
    │   └── etc/
    │       ├── mongodb.conf        # MongoDB configuration file
    │       └── mongodb.conf.sample # MongoDB configuration file sample
    └── ports/
        └── databases/
            ├── mongodb80/  # MongoDB 8.0 Port
            └── mongosh/    # MongoDB Shell Port
```

If you need to further configure MongoDB, you can modify its configuration file.

The MongoDB 8.0 configuration file is located at **/usr/local/etc/mongodb.conf**, and the configuration template file is located at **/usr/local/etc/mongodb.conf.sample**.

## Creating Users and Passwords

MongoDB does not enable access control by default; anyone can connect to and operate the database. It is recommended to create users and set passwords.

Use `mongosh` to connect to the local MongoDB service:

```sql
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bcb0e5337ba1c7d62d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> show dbs   // List all databases in the MongoDB instance
admin   180.00 KiB
config   92.00 KiB
local    72.00 KiB
test> use admin   // Switch to the admin database; you can also create and use other databases
switched to db admin
admin> db.createUser({   // Create a new user
...   user: 'admin',     // Custom username
...   pwd: '<strong-password>',           // Custom password; a strong password should be used
...   roles:[{
...     role: 'root',      // Grant superuser role, can manage all databases
...     db: 'admin'        // Specify the database where the user resides
...   }]
... })
{ ok: 1 }
admin> quit   // Exit the MongoDB shell
```

You can copy the following text directly to the command line:

```sql
db.createUser({
   user: 'admin',
   pwd: '<your-strong-password>',
   roles:[{
     role: 'root',
     db: 'admin'
   }]
 })
```

After creating the user, enable password authentication:

Edit the **/usr/local/etc/mongodb.conf** file:

Remove the comment symbol `#` before `#security:`, then add `authorization: enabled` on the next line, as shown below:

```ini
security:
  authorization: enabled   # Enable MongoDB user authentication and access control
```

Restart the MongoDB service for the configuration to take effect:

```sh
# service mongod restart
```

## Login Methods

After enabling access control, you need to log in using a username and password. The methods are as follows:

### Login Method ①

Use `mongosh` to connect to the local MongoDB service:

```sql
# mongosh mongodb://127.0.0.1:27017
Current Mongosh Log ID:	67bcb1bafa0080a2132d5665
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test> use admin // Must switch to the admin database first
switched to db admin
admin> db.auth('admin', '<your-password>') // Log in to the MongoDB database using username and password
{ ok: 1 }
admin>
```

### Login Method ②

Log in to MongoDB using the username `admin` and password `z`:

```sql
# mongosh -u admin -p z
Current Mongosh Log ID:	67bcb200cbc3b601fb2d5665
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
Using MongoDB:		8.0.4
Using Mongosh:		2.2.5
mongosh 2.4.0 is available for download: https://www.mongodb.com/try/download/shell

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>
```

## References

* Yang's Blog. Using MongoDB with BT Panel - Some Tutorials for PHP7\[EB/OL]. (2023-09-28)\[2026-03-25]. <https://yooer.me/%E5%AE%9D%E5%A1%94%E9%9D%A2%E6%9D%BF-%E4%BD%BF%E7%94%A8mongodb%E4%B8%80%E4%BA%9B%E6%95%99%E7%A8%8B-php7.html>. Provides detailed MongoDB installation and basic configuration, serving as a practical reference for this section.
* ChiFan\@Wo. Adding Administrators and Users in MongoDB\[EB/OL]. \[2026-03-25]. <https://chenyejun.github.io/blog/mongoDB/mongodbAddUser.html>. Systematically explains MongoDB user creation and role assignment, providing reference for permission management.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://book.bsdcn.org/ask/flat/chapter-35-database-management/di-35.5-jie-mongodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
