# Mongodb

> Administer MongoDB databases. Configure replica sets, sharding, and backups. Use when managing MongoDB deployments.

- Skill: `majiayu000/mongodb-3` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds add majiayu000/mongodb-3`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/mongodb-3/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- License: MIT
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/mongodb-3

---


# MongoDB

Administer MongoDB NoSQL databases.

## Installation & Setup

```bash
# Install
apt install mongodb-org

# Start service
systemctl start mongod

# Connect
mongosh

# Create user
use admin
db.createUser({
  user: "admin",
  pwd: "secret",
  roles: ["root"]
})
```

## Basic Operations

```javascript
// Create database and collection
use mydb
db.users.insertOne({ name: "John", email: "john@example.com" })

// Query
db.users.find({ name: "John" })
db.users.find().sort({ name: 1 }).limit(10)

// Index
db.users.createIndex({ email: 1 }, { unique: true })
```

## Replica Set

```javascript
// Initialize replica set
rs.initiate({
  _id: "myReplicaSet",
  members: [
    { _id: 0, host: "mongo1:27017" },
    { _id: 1, host: "mongo2:27017" },
    { _id: 2, host: "mongo3:27017" }
  ]
})
```

## Backup

```bash
# Backup
mongodump --out /backup/

# Restore
mongorestore /backup/
```

## Best Practices

- Use replica sets in production
- Implement proper indexing
- Enable authentication
- Regular backups with mongodump

