brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongoddocker run -d -p 27017:27017 --name telegram-mongodb mongo:7docker-compose up -d# اتصال به MongoDB
mongosh
# انتخاب دیتابیس
use telegram_service
# مشاهده collections
show collections
# مشاهده تمام پیامها
db.messages.find().pretty()
# مشاهده آخرین 10 پیام
db.messages.find().sort({date: -1}).limit(10)- دانلود از: https://www.mongodb.com/try/download/compass
- اتصال با:
mongodb://localhost:27017 - انتخاب دیتابیس:
telegram_service
db.messages.find({
"agent_id": 123
}).sort({ "date": -1 })db.messages.find({
"text": { $regex: "سلام", $options: "i" }
})db.messages.find({
"session_id": "your-session-id",
"chat_id": 123456789
}).sort({ "date": -1 })db.messages.countDocuments({ "agent_id": 123 })db.messages.aggregate([
{ $sort: { "date": -1 } },
{ $group: {
_id: "$chat_id",
lastMessage: { $first: "$$ROOT" }
}}
])db.messages.aggregate([
{ $match: { "agent_id": 123 } },
{ $group: {
_id: {
$dateToString: { format: "%Y-%m-%d", date: { $toDate: "$date" } }
},
count: { $sum: 1 }
}},
{ $sort: { "_id": -1 } }
])db.messages.aggregate([
{ $match: { "agent_id": 123 } },
{ $group: {
_id: "$is_outgoing",
count: { $sum: 1 }
}}
])db.messages.aggregate([
{ $match: { "agent_id": 123 } },
{ $group: {
_id: "$chat_id",
count: { $sum: 1 },
lastMessage: { $last: "$text" }
}},
{ $sort: { "count": -1 } },
{ $limit: 10 }
])# Backup کل دیتابیس
mongodump --db telegram_service --out /backup/
# Restore
mongorestore --db telegram_service /backup/telegram_service/mongoexport --db telegram_service --collection messages --out messages.jsonmongoimport --db telegram_service --collection messages --file messages.json// پاک کردن پیامهای بیشتر از 30 روز قبل
db.messages.deleteMany({
"created_at": {
$lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)
}
})// Index برای جستجوی سریعتر
db.messages.createIndex({ "text": "text" })
// Compound index
db.messages.createIndex({ "agent_id": 1, "date": -1 })db.messages.getIndexes()from pymongo import MongoClient
# اتصال
client = MongoClient("mongodb://localhost:27017")
db = client.telegram_service
# دریافت پیامها
messages = db.messages.find({"agent_id": 123}).limit(10)
for msg in messages:
print(msg['text'])
# ذخیره پیام
db.messages.insert_one({
"agent_id": 123,
"session_id": "uuid",
"text": "Hello",
"created_at": datetime.utcnow()
})# بررسی وضعیت
sudo systemctl status mongod
# مشاهده logs
sudo tail -f /var/log/mongodb/mongod.log# بررسی پورت
netstat -an | grep 27017
# تست اتصال
mongosh --eval "db.adminCommand('ping')"# بررسی فضای استفاده شده
du -sh /var/lib/mongodb
# فشردهسازی دیتابیس
db.runCommand({ compact: 'messages' })- ✅ همیشه از Index استفاده کنید برای query های پرتکرار
- ✅ Backup منظم از دیتابیس بگیرید
- ✅ از Aggregation Pipeline برای آنالیزهای پیچیده استفاده کنید
- ✅ پیامهای قدیمی را آرشیو یا حذف کنید
- ✅ از MongoDB Compass برای دیدن بصری دادهها استفاده کنید
برای سوالات بیشتر، به docs.mongodb.com مراجعه کنید.