一、数据库相关
注意:MongoDB在保存数据库的时候会自动创建数据库,且区分大小写。
关于固定集合:
- 大小限制:在创建时指定集合的大小,一旦达到限制,最早的文档会被移除。
- 高效存储:存储在预先分配的内存区域中,写入和读取操作非常高效,非常适合存储日志数据或其他需要保留最新数据的场景,也适用于需要高吞吐量和低延迟的应用场景。
- 顺序访问:只能按照插入顺序来获取数据,不支持索引或其他查询条件,不适合需要根据特定条件查询数据的场景。
- 不支持排序、分组和更新已存在的文档,只能删了再插入。
命令描述 | 命令示例 |
---|---|
显示数据库列表 | show dbs |
切换到指定数据库 | use database |
创建数据库 | use database; db.createCollection("collection") |
删除数据库 | db.dropDatabase() |
显示当前数据库 | db |
显示当前数据库的集合 | show collections |
创建集合 | db.createCollection("collection") |
创建固定集合 | db.createCollection("collection", {capped: true, size: 1024 * 1024}) |
删除集合 | db.collection.drop() |
备份数据库 | mongodump --db database --out /path/to/backup |
恢复数据库 | mongorestore --db database /path/to/backup/database |
显示数据库状态 | db.stats() |
显示集合状态 | db.collection.stats() |
二、插入数据
注意:所有数据以BSON格式存储,紧凑且扫描速度快。
命令描述 | 示例 |
定义文档 | document=({"name":"Tom", "sex":"男"}) |
插入文档 | db.collection.insertOne({ field1: value1, field2: value2 }) |
获取插入的结果 | db.collection.insertOne(doucment).WriteResult({"nInserted":1}) |
三、查询数据
命令描述 | 示例 |
查找文档 | db.collection.find({ field: value }) |
计数文档 | db.collection.countDocuments({ field: value }) |
排序文档 | db.collection.find().sort({ field: 1 }) // 升序, -1为降序 |
限制返回文档数量 | db.collection.find().limit(10) |
跳过文档 | db.collection.find().skip(5) |
条件查询 | db.collection.find({ field: { $gt: value } }) // 大于 |
范围查询 | db.collection.find({ field: { $gte: value1, $lte: value2 } }) // 大于等于和小于等于 |
逻辑查询 | db.collection.find({ $or: [{ field1: value1 }, { field2: value2 }] }) // 或 |
只返回指定字段 | db.collection.find({}, { field1: 1, field2: 1 }) |
只返回除这两个字段外的所有字段 | db.collection.find({}, { field1: 0, field2: 0 }) |
聚合查询 | db.collection.aggregate([{ group: { _id: "field", count: { $sum: 1 } } }]) |
文本搜索 | db.collection.find({ $text: { $search: "keyword" } }) |
正则表达式查询 | db.collection.find({ field: /pattern/ }) |
查询嵌套文档 | db.collection.find({ "field.nestedField": value }) |
查询数组 | db.collection.find({ field: { $in: [value1, value2] } }) |
查询日期范围 | db.collection.find({ dateField: { $gte: startDate, $lte: endDate } }) |
查询空字段 | db.collection.find({ field: { $exists: false } }) |
查询唯一字段 | db.collection.distinct("field") |
查询并返回部分字段 | db.collection.find({ field: value }, { field1: 1, field2: 1 }) |
查询并返回指定数量的字段 | db.collection.find({ field: value }).limit(10).forEach(function(doc) { print(doc.field1); }) |
四、常用条件操作符
操作符 | 描述 |
---|---|
$eq | 匹配指定字段等于给定值的文档 |
$ne | 匹配指定字段不等于给定值的文档 |
$gt | 匹配指定字段大于给定值的文档 |
$gte | 匹配指定字段大于等于给定值的文档 |
$lt | 匹配指定字段小于给定值的文档 |
$lte | 匹配指定字段小于等于给定值的文档 |
$in | 匹配指定字段值在给定数组中的文档 |
$nin | 匹配指定字段值不在给定数组中的文档 |
$exists | 匹配指定字段存在或不存在的文档 |
$type | 匹配指定字段类型为给定值的文档 |
$not | 对给定条件取反 |
$and | 匹配同时满足多个条件的文档 |
$or | 匹配满足任意一个条件的文档 |
$nor | 匹配不满足任何条件的文档 |
$regex | 匹配指定字段满足正则表达式模式的文档 |
$options | 用于指定正则表达式的选项,如i(不区分大小写)和m(多行模式) |
$elemMatch | 匹配数组字段中满足指定条件的元素的文档 |
$size | 匹配数组字段长度等于给定值的文档 |
$all | 匹配数组字段包含给定所有值的文档 |
$elemMatch | 匹配数组字段中满足指定条件的元素的文档 |
$mod | 匹配指定字段值模除给定值的结果为零的文档 |
$type | 匹配指定字段类型为给定值的文档 |
$expr | 在同一文档中使用聚合表达式进行匹配 |
$jsonSchema | 使用JSON模式验证文档的结构和内容 |
$geoWithin | 用于匹配指定地理区域内的文档 |
$geoIntersects | 用于匹配与指定地理区域相交的文档 |
$near | 用于执行地理空间查询的操作符 |
$nearSphere | 用于在球体上执行地理空间查询的操作符 |
$text | 用于执行全文索引搜索的操作符 |
$options | 用于指定全文索引搜索的选项,如language和caseSensitive |
$where | 使用JavaScript表达式进行查询 |
五、更新数据
注意:update函数接收参数为:criteria[查询条件],objNew[更新信息],options[upsert:存在则更新,不存在就创建,multi:是否更新所有的匹配文档]
命令描述 | 命令示例 |
---|---|
更新单个文档 | db.collection.updateOne({ field: value }, { $set: { fieldToUpdate: newValue } }) |
更新多个文档 | db.collection.updateMany({ field: value }, { $set: { fieldToUpdate: newValue } }) |
替换文档 | db.collection.replaceOne({ field: value }, { newDocument }) |
递增字段值 | db.collection.updateOne({ field: value }, { $inc: { fieldToIncrement: incrementValue } }) |
重命名字段 | db.collection.updateOne({ field: value }, { $rename: { oldField: newField } }) |
删除字段 | db.collection.updateOne({ field: value }, { $unset: { fieldToDelete: "" } }) |
添加数组元素 | db.collection.updateOne({ field: value }, { $push: { arrayField: newValue } }) |
删除数组元素 | db.collection.updateOne({ field: value }, { $pull: { arrayField: valueToRemove } }) |
更新数组元素 | db.collection.updateOne({ field: value, "arrayField.field": arrayValue }, { set: { "arrayField..fieldToUpdate": newValue } }) |
数组元素排序 | db.collection.updateOne({ field: value }, { $push: { arrayField: { $each: [newElement], $sort: { field: 1 } } } }) |
更新嵌套文档 | db.collection.updateOne({ field: value }, { $set: { "nestedField.fieldToUpdate": newValue } }) |
更新嵌套数组元素 | db.collection.updateOne({ field: value }, { set: { "nestedArrayField.[element].fieldToUpdate": newValue } }, { arrayFilters: [{ "element.field": value }] }) |
六、批处理
注意:执行批处理前要先告诉MongoDB是有序还是无序(有序需执行initializeOrderedBulkOp()初始化列表),执行有序列表发生异常时,不继续处理后面的数据,执行无序列表发生异常时,会继续处理后面的数据。
命令描述 | 命令示例 |
---|---|
批量插入文档 | db.collection.insertMany([{ field1: value1 }, { field2: value2 }]) |
批量更新文档 | db.collection.updateMany({ field: value }, { $set: { fieldToUpdate: newValue } }) |
批量删除文档 | db.collection.deleteMany({ field: value }) |
欢迎关注公众号【码上猎人】(*`▽´*)来呀,互相吹捧啊!