mongodb
// 查询整个表数据
db.chats.find()
db.getCollection('chats').find()
// 查询一条数据
db.chats.findOne({"userId": ObjectId("6465e9d8c54af94e4ce8c0e5")})
db.getCollection('chats').findOne({"userId": ObjectId("6465e9d8c54af94e4ce8c0e5")})
ObjectId字段的类型要在查询条件中进行转换
db.chats.find({"userId": ObjectId("6465e9d8c54af94e4ce8c0e5")})
// 删除操作(删除匹配的所有记录)
db.chats.remove({ "userId": "123" })
db.chats.deleteMany({ "userId": "123" })
// 删除 匹配的第一个文档
db.chats.remove({ "userId": "123" }, { justOne: true })
db.chats.deleteOne({ "userId": "123" })
// 删除整个集合
db.chats.drop()
// 修改数据集合
// 只修改一个字段
db.chats.updateOne({ "userId": "123" }, { $set: { "status": "active" } })
查询指定的字段
// 第二个参数就是查询列表中要显示的字段,_id: 0 则代表不显示_id字段
db.collection.find( {}, { field1: 1, field2: 1, _id: 0 } )
向已有文档中添加字段
// 添加两个字段 设置日期字段为当前时间
db.companies.updateMany(
{},
{
$currentDate: { "createdAt": { $type: "date" }},
$set: { "createBy" : "" }
}
)
// $set: { "specificDate": new Date("2023-06-06") }
添加字段
db.models.updateMany(
{},
{ $set: { "remark1" : "" } }
)
修改字段名
// 将字段remark1修改为remark2
db.models.updateMany(
{ },
{ $rename: { "remark1": "remark2" } }
)
移除字段
db.models.updateMany(
{},
{
$unset: { "remark1": "" }
}
)
mongodb 关联查询(通过主文档字段查询)
const data = await Chat.aggregate([
{
$match: {
userId: new mongoose.Types.ObjectId(userId)
}
},
{
$lookup: {
from: 'models',
localField: 'modelId',
foreignField: '_id',
as: 'modelInfo'
}
},
{
$project: {
_id: 1, // 设置为 0 表示不显示
title: 1, // 设置为 1 表示显示
modelId: 1, // 设置为 1 表示显示
updateTime: 1, // 设置为 1 表示显示
latestChat: 1,
'modelInfo.name': 1,
'modelInfo.remark': 1
}
},
{ $sort: { updateTime: -1 } },
{ $limit: 5 }
]);
创建字典分组和字典项集合
db.createCollection("dictionarygroup");
db.dictionarygroup.insertOne({
name: "",
code: "",
order: 0,
remark: "",
isDeleted: false,
createdAt: new Date(),
createdBy: ObjectId("617e957f64a6c3e07ad1bfb1"), // Modify the ObjectId value as per your requirement
updatedAt: new Date(),
updatedBy: ObjectId("617e957f64a6c3e07ad1bfb2"), // Modify the ObjectId value as per your requirement
isSystem: false
});
db.createCollection("dictionaryitem");
db.dictionaryitem.insertOne({
_id: ObjectId("617e957f64a6c3e07ad1bfb1"), // Modify the ObjectId value as per your requirement
name: "",
code: "",
dictionaryGroupId: ObjectId("617e957f64a6c3e07ad1bfb2"), // Modify the ObjectId value as per your requirement
parentId: ObjectId("617e957f64a6c3e07ad1bfb3"), // Modify the ObjectId value as per your requirement
order: 0,
isEnable: false,
remark: "",
isVisible: false,
isDeleted: false,
createdAt: new Date(),
createdBy: ObjectId("617e957f64a6c3e07ad1bfb4"), // Modify the ObjectId value as per your requirement
updatedAt: new Date(),
updatedBy: ObjectId("617e957f64a6c3e07ad1bfb5"), // Modify the ObjectId value as per your requirement
isSystem: false
});
创建题库集合
db.createCollection("questions");
db.questions.insertOne({
"question": "问题内容",
"answer": "答案内容",
"categoryId": ObjectId("617e957f64a6c3e07ad1bfb1"),
"themeId": ObjectId("617e957f64a6c3e07ad1bfb1"),
"order": "顺序",
"isenable": true,
"isDeleted": false,
"createdAt": new Date(),
"createdBy": ObjectId("617e957f64a6c3e07ad1bfb1"), // Modify the ObjectId value as per your requirement
"updatedAt": new Date(),
"updatedBy": ObjectId("617e957f64a6c3e07ad1bfb2"), // Modify the ObjectId value as per your requirement
})
考卷
db.createCollection("exampapers")
db.exampapers.insertOne({
"paperName": "考卷名称",
"paperLevel": "初级", // 可以是 "初级"、"中级" 或 "高级"
"categoryId": "分类选择",
"themeChoices": ["主题1", "主题2"], // 一个数组,包含了几个主题
"score": 0, // 分数
"questionCount": 10,
"isDeleted": false,
"createdAt": new Date(),
"createdBy": ObjectId("617e957f64a6c3e07ad1bfb1"), // Modify the ObjectId value as per your requirement
"updatedAt": new Date(),
"updatedBy": ObjectId("617e957f64a6c3e07ad1bfb2"), // Modify the ObjectId value as per your requirement
})
考卷对应的考题和作答情况
db.createCollection("examanswers")
db.examanswers.insertOne({
"paper_id": ObjectId("617e957f64a6c3e07ad1bfb1"),
"question_id": ObjectId("617e957f64a6c3e07ad1bfb1"),
"answerContent": "回答内容",
"gptContent": "gpt内容",
"score": 0, // 分数
"isDeleted": false,
"createdAt": new Date(),
"createdBy": ObjectId("617e957f64a6c3e07ad1bfb1"), // Modify the ObjectId value as per your requirement
"updatedAt": new Date(),
"updatedBy": ObjectId("617e957f64a6c3e07ad1bfb2"), // Modify the ObjectId value as per your requirement
})
通过正则模糊查询
db.models.find(
{
remark: {
$regex: `7\\.29`,
$options: 'ig'
}
}
)