| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
MongoDB 基本概念 : 文档、集合、数据库
SQL 和 MongoDB 术语比较
| SQL 术语 / 概念 | MongoDB 术语 / 概念 | 解释 / 说明 |
|---|---|---|
| database | database | 数据库 |
| table | collection | 数据库表 / 集合 |
| row | document | 数据记录行 / 文档 |
| column | field | 数据字段 / 域 |
| index | index | 索引 |
| table joins | 表链接 / MongoDB 不支持 | |
| primary key | primary key | 主键, MongoDB自动将 _id 字段设置为主键 |
MongoDB 数据库初步 : 一个 MongoDB 中可以创建多个数据库.MongoDB 的单个实例可以容纳多个独立的数据库,每一个都有自己的集合和权限,不同的数据库也放置在不同的文件中
文档 : 一组 键值对 (key=>value) 即 (BSON) . MongoDB 文档不需要设置相同的字段,并且相同的字段不需要相同的数据类型
集合 : MongoDB 的文档组, 类似于 RDBMS (关系型数据库) 中的表格 (table)
合法的集合名
查看已有集合 : show tables 或 show collections
MongoDB 创建集合
语法格式
db.createCollection(name, options)
参数
name : 集合名称
options 可选参数, 指定有关内存的大小以及索引的选项
| 字段 | 类型 | 描述 |
|---|---|---|
| capped | 布尔 | (可选)如果为 True : 创建固定集合 (有固定大小的集合), 达到最大值时自动覆盖最早的文档 当该值为 True 时,必需指定 size 参数 |
| autoIndexId | 布尔 | (可选) 如果为 True 自动在 _id 字段创建索引,默认为 True |
| size | 数值 | (可选) 为固定集合指定一个最大值(以字节计) 如果capped 为 True,也可以指定该字段 |
| max | 数值 | (可选) 指定固定集合中包含文档的最大数量 |
插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段
隐式创建集合 : db.collection_name.insert({key: value})
MongoDB 删除集合
语法格式
db.collection.drop()
MongoDB 使用 insert() 方法向集合中插入文档 : db.集合名.insert(插入内容)
查看已插入文档 : db.集合名.find()
两种单条插入方法
直接插入
> use python
switched to db python
> db.python.find()
> db.python.insert({title: '爬虫', by: 'offcn'})
WriteResult({ "nInserted" : 1 })
> db.python.find() })
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫", "by" : "offcn" }
定义变量后由变量插入
document = {title: '网络爬虫',by: 'text'} { "title" : "网络爬虫", "by" : "text" } db.python.insert(document) WriteResult({ "nInserted" : 1 }) db.python.find() xt'} { "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫", "by" : "offcn" } { "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "by" : "text" }
多条插入文档 : db.集合名.insert([document1, document2, ...])
MongoDB 中查询表达式相当于 sql 中的 where 子句查询条件,可以用来过滤数据
等于 : {field:value} 表示 field = value
小于 : {field:{$lt:value}} 表示 field < value
大于 : {field:{$gt:value}} 表示 field > value
小于等于 : {field:{$lte:value}} 表示 field <= value
大于等于 : {field:{$gte:value}} 表示 field >= value
不等于 : {field:{$ne:value}} 表示 field != value
$not : {$not: [{条件1}, {条件2}, ...]} 多个条件都不满足
实例: 取出不属于第三栏且不属于第十一栏目的商品 (与 nin 实例结果相同)
db.goods.find({$not:[{cat_id:3}, {cat_id:11}]},{_id:0,goods_name:1,cat_id:1}).pretty()
$and : {$and: [{条件1}, {条件2}, ...]} 多个条件同时满足,相当于逻辑中的且
$or : {$or: [{条件1}, {条件2}, ...]} 多个条件满足其中之一,相当于逻辑关系中的或
$nin : { field: { $nin: [ <value1>, <value2> ... <valueN> ]} } 该 field 值不在指定范围 或 不存在
+ 实例: 取出不属于第三栏且不属于第十一栏目的商品
```mariadb
db.goods.find({cat_id:{$nin: [3, 11]}},{_id:0,goods_name:1,cat_id:1}).count()
// 14
db.goods.find({cat_id:{$nin: [3, 11]}},{_id:0,goods_name:1,cat_id:1}).pretty()
```
update() 方法 : 更新已存在的文档
语法格式
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
参数说明
新文档替换旧文档
MongoDB update 方法中的 update 条件配合一定的更新表达式达到文档更新的目的
如果 update 条件对应参数是一个文档,则该 update 方法的实际操作其实是 : 用 updata 条件对应的 文档替换 query 查询出来的文档
命令格式
> db.python.update({title:"爬虫"}, {title:"爬虫测试", by:"测试"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.python.find() 试"}) "测试"})
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "by" : "测试" }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "by" : "text" }
更新表达式参数
$set : 修改某列的值:有则更新,无则插入
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "by" : "测试" }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "by" : "text" }
> db.python.update({title:"爬虫测试"}, {$set:{by:"李四"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.python.find() 四"}})
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "by" : "李四" }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "by" : "text" }
$unset : 删除某个列
默认删除匹配数据的第一个删除,需要删除更多的话,需要将 multi 参数 设为 true : db.python.update({title:"爬虫测试"}, {$set:{by:"李四"}}, {multi:true})
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四", "uid" : "01", "price" : 160 }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "后端开发", "创作者" : "AngleLiu", "uid" : "01", "price" : 180 }
> db.python.update({uid:"01"}, {$unset: {price: 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四", "uid" : "01" }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "后端开发", "创作者" : "AngleLiu", "uid" : "01", "price" : 180 }
$rename : 重命名某个列
db.python.find() { "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "by" : "李四" } { "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "创作者" : "text" } db.python.update({title:"爬虫测试"}, {$rename:{by:"创作者"}}, {multi:true}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) db.python.find() { "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四" } { "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "网络爬虫", "创作者" : "text" }
+ `$inc` : 增长某个列(做减法)
```mariadb
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四", "uid" : "01", "price" : 200 }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "后端开发", "创作者" : "AngleLiu", "uid" : "01", "price" : 200 }
> db.python.update({uid:"01"}, {$inc:{price:-20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四", "uid" : "01", "price" : 180 }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "后端开发", "创作者" : "AngleLiu", "uid" : "01", "price" : 200 }
> db.python.update({uid:"01"}, {$inc:{price:-20}}, {multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.python.find()
{ "_id" : ObjectId("5facdf85ae691aca255d6256"), "title" : "爬虫测试", "创作者" : "李四", "uid" : "01", "price" : 160 }
{ "_id" : ObjectId("5facdfd9ae691aca255d6257"), "title" : "后端开发", "创作者" : "AngleLiu", "uid" : "01", "price" : 180 }
remove()方法 : 移除集合中数据
基本语法格式
db.集合名.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
参数
MongoDB 查询文档使用 find() 方法
find() 方法: 以非结构的方式显示所有文档
基本语法格式
db.集合名.find(query, projection)
参数
pretty() 方法 : 结构化显示文档
基本语法格式 : db.集合名.find().pretty()
> db.python.find().pretty()
{
"_id" : ObjectId("5facdf85ae691aca255d6256"),
"title" : "爬虫测试",
"创作者" : "李四"
}
{
"_id" : ObjectId("5facdfd9ae691aca255d6257"),
"title" : "网络爬虫",
"创作者" : "text"
}
limit() 方法: 读取指定数量的数据记录,接受一个数字参数,指定从 MongoDB 中读取记录条数
基本语法 : db.集合名.find().limit(Number)
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1}).limit(3).pretty()
{
"_id" : ObjectId("5fad00c22b27f6c5659ca5e1"),
"cat_id" : 4,
"goods_name" : "KD876"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5ed"),
"cat_id" : 4,
"goods_name" : "诺基亚5800XM"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5"
}
skip() 方法: 跳过指定数量的数据,接受一个数字参数做为跳过记录条数
基本语法 : db.集合名.find().limit(Number1).skip(Number2)
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1}).limit(3).skip(2).pretty()
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5"
}
sort() 方法 : 对数据进行排序,可以通过参数指定排序的字段,并使用 1 (升序) 和 -1 (降序) 指定排序方式
基本语法 : db.集合名.find().sort({KEY:1})
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1}).limit(3).pretty()
{
"_id" : ObjectId("5fad00c22b27f6c5659ca5e1"),
"cat_id" : 4,
"goods_name" : "KD876"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5ed"),
"cat_id" : 4,
"goods_name" : "诺基亚5800XM"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5"
}
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1}).limit(3).skip(2).pretty()
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5"
}
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1}).limit(3).pretty()
{
"_id" : ObjectId("5fad00c22b27f6c5659ca5e1"),
"cat_id" : 4,
"goods_name" : "KD876"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5ed"),
"cat_id" : 4,
"goods_name" : "诺基亚5800XM"
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5"
}
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1, shop_price:1}).limit(3).pretty()
{
"_id" : ObjectId("5fad00c22b27f6c5659ca5e1"),
"cat_id" : 4,
"goods_name" : "KD876",
"shop_price" : 1388
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5ed"),
"cat_id" : 4,
"goods_name" : "诺基亚5800XM",
"shop_price" : 2625
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5",
"shop_price" : 2878
}
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1, shop_price:1}).limit(3).sort({KEY:1}).pretty()
{
"_id" : ObjectId("5fad00c22b27f6c5659ca5e1"),
"cat_id" : 4,
"goods_name" : "KD876",
"shop_price" : 1388
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5ed"),
"cat_id" : 4,
"goods_name" : "诺基亚5800XM",
"shop_price" : 2625
}
{
"_id" : ObjectId("5fad01002b27f6c5659ca5f1"),
"cat_id" : 4,
"goods_name" : "夏新T5",
"shop_price" : 2878
}
count() 方法 : 对数据数量进行统计
基本语法 : db.集合名.find().count()
> db.goods.find({cat_id:4},{cat_id:1, goods_name:1, shop_price:1}).count()
3
> db.goods.find({cat_id:4}).count()
3
> db.goods.find().count()
31
mongodb 数据库文件夹下的各种工具介绍
将 mongodb 中数据导出为其它文件数据
mongoexport.exe : 数据库导出工具
-type : 导出类型 (csv、txt、json等)
-o : 输出文件名 ,可以添加路径
-d : 导出数据库名
-c : 导出集合名
-f : 导出列名 (字段名),多个字段以逗号隔开
-q : 查询表达式
--pretty : 以漂亮的打印格式JSON输出文档
mongoimport.exe : 数据库导入工具
| Back | FazBrowse Home | New Git URL |