Install
openclaw skills install mongodb-skillMongoDB 文档数据库管理技能。通过自然语言查询、管理 MongoDB,支持文档查询、聚合操作、索引管理、地理空间查询等功能。当用户提到 MongoDB、NoSQL、文档数据库时使用此技能。
openclaw skills install mongodb-skill通过自然语言,轻松管理 MongoDB,利用其强大的文档操作能力!
Ubuntu/Debian:
sudo apt update
sudo apt install mongodb-clients
macOS:
brew install mongodb-community-shell
Python 客户端(推荐):
pip install pymongo
使用 mongosh:
mongosh "mongodb://localhost:27017/your_database"
使用连接字符串:
mongodb://username:password@localhost:27017/database
// 查询年龄大于 25 的用户
db.users.find({ age: { $gt: 25 } })
// 模糊查询(正则表达式)
db.articles.find({ title: { $regex: /人工智能/i } })
// 数组字段查询(包含特定值)
db.products.find({ tags: "electronics" })
// 多条件查询
db.users.find({
age: { $gte: 18, $lte: 35 },
status: "active"
})
// 指定返回字段
db.users.find(
{ age: { $gt: 25 } },
{ name: 1, email: 1, _id: 0 }
)
// 统计每个分类的文章数量
db.articles.aggregate([
{ $group: {
_id: "$category",
count: { $sum: 1 }
}
},
{ $sort: { count: -1 } }
])
// 计算用户的平均订单金额
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$user_id",
total_spent: { $sum: "$amount" },
order_count: { $sum: 1 }
}
},
{ $addFields: {
avg_order: { $divide: ["$total_spent", "$order_count"] }
}
},
{ $sort: { total_spent: -1 } }
])
// 时间序列分析(按天统计)
db.orders.aggregate([
{
$group: {
_id: {
$dateToString: {
format: "%Y-%m-%d",
date: "$created_at"
}
},
total_amount: { $sum: "$amount" },
count: { $sum: 1 }
}
},
{ $sort: { _id: 1 } }
])
// 复杂聚合(多表 JOIN)
db.orders.aggregate([
{
$lookup: {
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user_info"
}
},
{ $unwind: "$user_info" },
{
$project: {
order_id: "$_id",
amount: 1,
user_name: "$user_info.name",
user_email: "$user_info.email"
}
}
])
// 更新单个文档
db.users.updateOne(
{ _id: ObjectId("123") },
{ $set: { last_login: new Date() } }
)
// 批量更新
db.users.updateMany(
{ status: "inactive" },
{ $set: { archived: true } }
)
// 添加字段
db.users.updateMany(
{},
{ $set: { created_at: new Date() } }
)
// 数组操作(添加元素)
db.products.updateOne(
{ _id: ObjectId("123") },
{ $push: { tags: "new_tag" } }
)
// 数组操作(删除元素)
db.products.updateOne(
{ _id: ObjectId("123") },
{ $pull: { tags: "old_tag" } }
)
// 嵌套文档更新
db.users.updateOne(
{ _id: ObjectId("123") },
{ $set: { "profile.address": "新地址" } }
)
// 数组元素更新
db.orders.updateOne(
{ _id: ObjectId("123"), "items.product_id": ObjectId("456") },
{ $set: { "items.$.quantity": 10 } }
)
// 查询数组包含特定值
db.posts.find({ tags: "javascript" })
// 查询数组包含多个值中的任意一个
db.posts.find({ tags: { $in: ["javascript", "python"] } })
// 查询数组包含所有指定值
db.posts.find({ tags: { $all: ["javascript", "mongodb"] } })
// 查询数组长度
db.posts.find({ tags: { $size: 3 } })
// 查询数组的第 N 个元素
db.posts.find({ "tags.0": "javascript" })
// 创建地理索引
db.places.createIndex({ location: "2dsphere" })
// 查询距离某点 5 公里内的地点
db.places.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [116.4074, 39.9042] // [经度, 纬度]
},
$maxDistance: 5000 // 5 公里
}
}
})
// 计算距离
db.places.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [116.4074, 39.9042]
},
distanceField: "distance",
maxDistance: 5000,
spherical: true
}
}
])
// 查询特定区域内的地点
db.places.find({
location: {
$geoWithin: {
$polygon: [
[116.3, 39.9],
[116.5, 39.9],
[116.5, 40.0],
[116.3, 40.0]
]
}
}
})
// 创建单字段索引
db.users.createIndex({ email: 1 })
// 创建复合索引
db.orders.createIndex({ user_id: 1, created_at: -1 })
// 创建唯一索引
db.users.createIndex({ username: 1 }, { unique: true })
// 创建文本索引(全文搜索)
db.articles.createIndex({ title: "text", content: "text" })
// 全文搜索
db.articles.find(
{ $text: { $search: "人工智能" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })
// 查看索引
db.users.getIndexes()
// 删除索引
db.users.dropIndex("email_1")
完整备份:
mongodump --uri="mongodb://localhost:27017/your_database" \
--out=backup_$(date +%Y%m%d)
仅备份单个集合:
mongodump --uri="mongodb://localhost:27017/your_database" \
--collection=users --out=backup_users
使用查询条件备份:
mongodump --uri="mongodb://localhost:27017/your_database" \
--query='{ "status": "active" }' --out=backup_active
恢复完整备份:
mongorestore --uri="mongodb://localhost:27017/your_database" backup_20260323
恢复单个集合:
mongorestore --uri="mongodb://localhost:27017/your_database" \
backup_20260323/your_database/users.bson
嵌入模式:
// 适合 1:N 关系,N 较小
{
_id: ObjectId("..."),
title: "文章标题",
comments: [
{ user_id: ObjectId("..."), content: "评论内容", created_at: Date }
]
}
引用模式:
// 适合 N:N 关系或 N 较大
{
_id: ObjectId("..."),
title: "文章标题",
comment_ids: [ObjectId("..."), ObjectId("...")]
}
// 使用事务处理多集合操作
session = db.getMongo().startSession()
session.startTransaction()
try {
db.users.updateOne(
{ _id: user_id },
{ $inc: { balance: -100 } },
{ session }
)
db.orders.insertOne(
{ user_id, amount: 100, created_at: new Date() },
{ session }
)
session.commitTransaction()
} catch (error) {
session.abortTransaction()
throw error
}
// 监听集合的变更
changeStream = db.users.watch()
changeStream.on('change', (next) => {
console.log(next)
// 根据 next.operationType 处理不同事件
})
.limit()$where 查询 - 使用 MongoDB 操作符reIndex()用户: "统计每个用户最近的订单金额"
AI: "使用聚合管道查询每个用户的订单统计"
db.orders.aggregate([
{ $match: { status: "completed" } },
{
$group: {
_id: "$user_id",
last_order_date: { $max: "$created_at" },
total_amount: { $sum: "$amount" },
order_count: { $sum: 1 }
}
},
{ $sort: { total_amount: -1 } }
])
用户: "查询距离天安门 5 公里内的餐厅"
AI: "使用地理空间索引查询附近地点"
db.places.find({
type: "restaurant",
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [116.3974, 39.9093] // 天安门坐标
},
$maxDistance: 5000
}
}
})
用户: "给所有订单添加一个 processed 字段"
AI: "批量更新订单文档"
db.orders.updateMany(
{ processed: { $exists: false } },
{ $set: { processed: false } }
)
开始使用: 告诉我你的 MongoDB 操作需求,我会帮你生成相应的查询!🚀