- 关于数据库和集合的操作
命令 | 描述 |
---|---|
show dbs | 查看所有数据库(存在数据的数据库) |
db | 查看用户当前所在数据库 |
use db_name | 切换到db_name数据库 |
db.dropDatabase | 删除用户当前所在的数据库 |
show collections | 查看当前数据库所有集合(即表) |
db.createCollection("集合名") | 创建集合 |
db.集合名. | 删除集合 |
# 查看所有数据库
> show dbs
admin 0.000GB
config 0.000GB
kaikeba 0.000GB
local 0.000GB
test 0.000GB
# 查看用户当前所在数据库
> db
kaikeba
# 切换到test数据库
> use test
switched to db test
# 删除用户当前所在的数据库
> show collections
game_integral_anchor_acu_month
test1
# 在test数据库中执行删库操作后,没有看到test数据库了
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
kaikeba 0.000GB
local 0.000GB
# 创建集合:gonglve
> db
test
> db.createCollection("gonglve")
{ "ok" : 1 }
> show collections
gonglve
- 增:db.集合的名字.insert({})
db.gl.insert({"name" : "乾隆", "like" : "狗", "age" : 20, "gender": true})
- 删:db.集合名字.remove({删除条件})
默认符合条件的数据都会删掉
# 将年龄为15的数据都删除
> db.gl.remove({"age":15})
WriteResult({ "nRemoved" : 2 })
如果只想删除符合条件的第一个,可以:db.集合.remove({删除条件},{justOne:true})
db.集合.remove({}),即remove后不加任何条件时,会删除该表的所有的数据
- 改:db.集合名字.update({查询条件},{$set:修改内容})
- 默认只修改符合条件的第一个 *
> db.gl.update({"gender":true},{$set:{"age": 22}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
要修改符合条件的所有数据,需要添加{multi :true},即db.集合名字.update({查询条件},{$set:修改内容},{multi :true})
- 查:db.集合名字.find({条件})
初始插入的数据
db.gl.insert({"_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender": true})
db.gl.insert({"_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" :18 , "gender": false})
db.gl.insert({"_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender": false})
db.gl.insert({"_id" : 4, "name" : "高贵妃", "like" : "鸡", "age" : 45, "gender": true})
db.gl.insert({"_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender": true})
db.gl.insert({"_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender": true})
db.gl.insert({"_id" : 7, "name" : "富察皇后", "like" : "鸡", "age" : 60, "gender": true})
数据库初始数据:
> db.gl.find()
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" : 18, "gender" : false }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 4, "name" : "高贵妃", "like" : "鸡", "age" : 45, "gender" : true }
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 7, "name" : "富察皇后", "like" : "鸡", "age" : 60, "gender" : true }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
(1)比较运算:
运算符 | 英文扩展 | 描述 | 示例 |
---|---|---|---|
$lt | less than | 小于 | db.gl.find({"age" : {$lt: 22}}) |
$lte | less than equal | 小于等于 | db.gl.find({"age" : {$lte: 22}}) |
$gt | greater than | 大于 | db.gl.find({"age" : {$gt: 45}}) |
$gte | greater than equal | 大于等于 | db.gl.find({"age" : {$gte: 40}}) |
$ne | not equal | 不等于 | db.gl.find({"age" : {$ne: 45}}) |
(2)逻辑运算符
运算符 | 示例 |
---|---|
$and | db.gl.find({gt:20}}, {"gender" : true}]}),其实find(默认多条件就是且的关系),该示例跟db.gl.find({"age" : {$gt:20}, "gender" : true})是等同的 |
$or | db.gl.find({$or : [{"gender" : false}, {"like" : "狗"}]}) |
混合使用 | db.gl.find({or : [{"age" : {$gt : 28}}, {"like" : "荷兰鼠"}]}]}),查询年龄大于28岁,或者喜欢荷兰鼠;并且必须满足gender为true |
(3)范围运算符
运算符 | 示例 |
---|---|
in | db.gl.find({"like" : {$in : ["狗", "鸡"]}}) |
nin | db.gl.find({"age" : {$nin : [45, 18]}}) |
(4)正则匹配:/......./或regex : "小"}}),查询结果如下:
> db.gl.find({"name" : /小/})
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
> db.gl.find({"name" : {$regex : "小"}})
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
如果要匹配时忽略大小写,可以 /.../i 或 regex : "x", $option:"i"}})都可以查询到name包含X或x的数据
(5)定义函数:$where : function(){函数操作}
注意:函数是用的是js的语法
示例: db.gl.find({$where:function(){ return this.age > 45 }})
(6)限制显示个数,显示前几个:limit()
db.gl.find({}).limit(3)
(7)跳过前几个开始看:skip()
db.gl.find({}).skip(2)
(8)投影:结果只显示需要的字段
> db.gl.find({},{"name":1,"age":1})
{ "_id" : 1, "name" : "乾隆", "age" : 22 }
{ "_id" : 2, "name" : "魏璎珞", "age" : 18 }
{ "_id" : 3, "name" : "令贵妃", "age" : 28 }
{ "_id" : 4, "name" : "高贵妃", "age" : 45 }
{ "_id" : 6, "name" : "小张子", "age" : 45 }
{ "_id" : 7, "name" : "富察皇后", "age" : 60 }
{ "_id" : 5, "name" : "小李子", "age" : 16 }
(9)排序:sort({key1:1, key2:-1})即按照key1升序,key2降序排序,字段的value设置为1时表示升序,为-1时表示按照降序排序
# 按照age升序,_id降序进行排序
> db.gl.find().sort({"age":1,"_id":-1})
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
{ "_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" : 18, "gender" : false }
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 4, "name" : "高贵妃", "like" : "鸡", "age" : 45, "gender" : true }
{ "_id" : 7, "name" : "富察皇后", "like" : "鸡", "age" : 60, "gender" : true }
(10)统计数据个数:count()
db.gl.find({"gender" : true}).count()
(11)字段去重:distinct(key)
> db.gl.distinct("like")
[ "狗", "猫", "鸡", "荷兰鼠" ]
- 管道查询
(1)$group:将集合中的文档分组
表达式 | 描述 | 示例 |
---|---|---|
$sum | 计算总和 | db.gl.aggregate([{gender",age_sum:{age"}}}]) |
$avg | 计算平均值 | db.gl.aggregate([{gender",age_avg:{age"}}}]) |
$min | 获取集合中所有文档对应值得最小值 | db.gl.aggregate([{like", age_min:{age"}}}]) |
$max | 获取集合中所有文档对应值得最大值 | db.gl.aggregate([{gender",age_max:{age"}}}]) |
$push | 在结果文档中插入值到一个数组中 | db.gl.aggregate([{gender",name:{name"}}}]) |
$addToSet | 在结果文档中插入值到一个数组中,但不创建副本 | |
$first | 根据资源文档的排序获取第一个文档数据 | db.gl.aggregate([{gender",age_first:{age"}}}]) |
$last | 根据资源文档的排序获取最后一个文档数据 | db.gl.aggregate([{gender",age_last:{age"}}}]) |
注意$push的使用,它可以显示分组内的其他数据的内容,即有哪些数据是在这个分组内的,如下:
> db.gl.find()
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" : 18, "gender" : false }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 4, "name" : "高贵妃", "like" : "鸡", "age" : 45, "gender" : true }
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 7, "name" : "富察皇后", "like" : "鸡", "age" : 60, "gender" : true }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
> db.gl.aggregate([{$group:{_id:"$gender",age_first:{$push:"$name"}}}])
{ "_id" : false, "age_first" : [ "魏璎珞", "令贵妃" ] }
{ "_id" : true, "age_first" : [ "乾隆", "高贵妃", "小张子", "富察皇后", "小李子" ] }
# 说明:可以看出,$push会返回指定的分组的内容
(2)match:用于过滤数据,只输出符合条件的文档。match使用MongoDB的标准查询操作。
> db.gl.aggregate([{$match:{"age":{$gt:20}}}])
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 4, "name" : "高贵妃", "like" : "鸡", "age" : 45, "gender" : true }
{ "_id" : 6, "name" : "小张子", "like" : "狗", "age" : 45, "gender" : true }
{ "_id" : 7, "name" : "富察皇后", "like" : "鸡", "age" : 60, "gender" : true }
单独使用看不出效果,group一起使用:
# 统计20岁以上的男性、女性的人数
> db.gl.aggregate([{$match:{"age":{$gt:20}}}, {$group:{_id:"$gender",count:{$sum:1}}}])
{ "_id" : false, "count" : 1 }
{ "_id" : true, "count" : 4 }
# 注意:count:{$sum:1}中的1表示将数据当作1处理,表示1条数据,即+1,达到数据统计的效果,用于统计数据出现的个数
(3)$project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
# 年龄大于16岁的人, 按照性别分组,显示人的年龄之和,年龄平均值
> db.gl.aggregate([{$match:{"age":{$gt:16}}}, {$group:{_id:"$gender", age_sum:{$sum:"$age"}, age_avg:{$avg:"$age"}}}])
{ "_id" : false, "age_sum" : 46, "age_avg" : 23 }
{ "_id" : true, "age_sum" : 172, "age_avg" : 43 }
# 只显示年龄之和一列,年龄平均不显示,通过project设置age_sum显示(即为1)
> db.gl.aggregate([{$match:{"age":{$gt:16}}}, {$group:{_id:"$gender", age_sum:{$sum:"$age"}, age_avg:{$avg:"$age"}}}, {$project:{"age_sum":1}}])
{ "_id" : false, "age_sum" : 46 }
{ "_id" : true, "age_sum" : 172 }
(4)$sort:将输入文档排序后输出,1为升序,-1为降序
# 45岁之下的宠物分类中第一个数据的年龄进行降序排序
> db.gl.aggregate([{$match:{age:{$lt:45}}}, {$group:{_id:"$like",first_age:{$first:"$age"}}}, {$sort:{first_age:-1}}])
{ "_id" : "狗", "first_age" : 22 }
{ "_id" : "猫", "first_age" : 18 }
{ "_id" : "荷兰鼠", "first_age" : 16 }
(5)$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
# 按照性别分组,取出喜欢的宠物, 拆分文档
> db.gl.aggregate([{$group:{_id:"$gender", like_list:{$push:"$like"}}}])
{ "_id" : false, "like_list" : [ "猫", "猫" ] }
{ "_id" : true, "like_list" : [ "狗", "鸡", "狗", "鸡", "荷兰鼠" ] }
> db.gl.aggregate([{$group:{_id:"$gender", like_list:{$push:"$like"}}}, {$unwind:"$like_list"}])
{ "_id" : false, "like_list" : "猫" }
{ "_id" : false, "like_list" : "猫" }
{ "_id" : true, "like_list" : "狗" }
{ "_id" : true, "like_list" : "鸡" }
{ "_id" : true, "like_list" : "狗" }
{ "_id" : true, "like_list" : "鸡" }
{ "_id" : true, "like_list" : "荷兰鼠" }
(6)$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
$limit:用来限制MongoDB聚合管道返回的文档数。
> db.gl.aggregate([{$match:{age:{$lt:45}}}])
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" : 18, "gender" : false }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
# 跳过前2个数据
> db.gl.aggregate([{$match:{age:{$lt:45}}}, {$skip:2}])
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
{ "_id" : 5, "name" : "小李子", "like" : "荷兰鼠", "age" : 16, "gender" : true }
# 只显示前3个数据:
> db.gl.aggregate([{$match:{age:{$lt:45}}}, {$limit:3}])
{ "_id" : 1, "name" : "乾隆", "like" : "狗", "age" : 22, "gender" : true }
{ "_id" : 2, "name" : "魏璎珞", "like" : "猫", "age" : 18, "gender" : false }
{ "_id" : 3, "name" : "令贵妃", "like" : "猫", "age" : 28, "gender" : false }
- 索引查询
索引是提高查询查询效率最有效的手段。索引是一种特殊的数据结构,索引以易于遍历的形式存储了数据的部分内容(如:一个特定的字段或一组字段值),索引会按一定规则对存储值进行排序,而且索引的存储位置在内存中,所在从索引中检索数据会非常快。如果没有索引,MongoDB必须扫描集合中的每一个文档,这种扫描的效率非常低,尤其是在数据量较大时。
参考资料:https://itbilu.com/database/mongo/E1tWQz4_e.html
(1)查看集合中的索引:
db.集合名.getIndexes()
(2)创建索引:
db.集合名.ensureIndex({key1:1,key2:-1})
(3)删除索引
db.集合名.dropIndex(key)
其中,key必须要遵从索引那里的名称,不是创建索引时的key。
# 创建100000条数据,用于索引的实验
> for(var i = 0; i < 100000; i++){
... db.sy.insert({_id:i, name: "name"+i, age:i})
... }
WriteResult({ "nInserted" : 1 })
> db.sy.find().count()
100000
# 数据库自动为_id创建的索引
> db.sy.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "suoyin.sy"
}
]
# 常规数据库查询"name8888"时,耗时executionTimeMillisEstimate为148毫秒
> db.sy.find({name : "name8888"}).explain('executionStats')
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "suoyin.sy",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "name8888"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "name8888"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 148,
"totalKeysExamined" : 0,
"totalDocsExamined" : 100000,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"name" : {
"$eq" : "name8888"
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 126,
"works" : 100002,
"advanced" : 1,
"needTime" : 100000,
"needYield" : 0,
"saveState" : 783,
"restoreState" : 783,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 100000
}
},
"serverInfo" : {
"host" : "LAPTOP-OE87D4BO",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1
}
# 为字段name创建索引name_1
> db.sy.ensureIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.sy.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "suoyin.sy"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "suoyin.sy"
}
]
# 创建了name字段的索引后,查询"name8888"数据的耗时executionTimeMillisEstimate为6毫秒了,查询效率提高了大概24倍
> db.sy.find({name : "name8888"}).explain('executionStats')
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "suoyin.sy",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "name8888"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"name" : 1
},
"indexName" : "name_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"name" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"name8888\", \"name8888\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 6,
"totalKeysExamined" : 1,
"totalDocsExamined" : 1,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"name" : 1
},
"indexName" : "name_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"name" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"name8888\", \"name8888\"]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "LAPTOP-OE87D4BO",
"port" : 27017,
"version" : "3.6.3",
"gitVersion" : "9586e557d54ef70f9ca4b43c26892cd55257e1a5"
},
"ok" : 1
}
# 删除索引
# 目前有_id_和name_1索引,其中name_1索引就是上面根据name字段自创的索引,在数据库中名称是name_1,所以删除该索引的时候,需要注意其在数据库中的名称
> db.sy.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "suoyin.sy"
},
{
"v" : 2,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "suoyin.sy"
}
]
# 删除自己创建的name索引,不能直接用name,直接用的话数据库会找不到该索引
> db.sy.dropIndex("name")
{
"nIndexesWas" : 2,
"ok" : 0,
"errmsg" : "index not found with name [name]",
"code" : 27,
"codeName" : "IndexNotFound"
}
# 要用数据库中的name_1
> db.sy.dropIndex("name_1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.sy.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "suoyin.sy"
}
]
- 数据备份和恢复
可以参考该文档:
https://segmentfault.com/a/1190000006236494
https://blog.csdn.net/lidengchun/article/details/78749279
- pymongo的基本操作
import pymongo
from datetime import datetime
# 1、连接数据库
mongo = pymongo.MongoClient(host='127.0.0.1',port=27017)
# 2、创建数据库
database = mongo['kaikeba']
# 3、创建集合
collection = database['gl']
# 4、操作
# ***************** 查询数据 *****************
#查询所有数据
datas = collection.find()
# find()可以查询到多个数据,返回的是'pymongo.cursor.Cursor'对象
datas = collection.find({"gender" : True, "age" : {"$gt" : 40}})
for data in datas:
print(data)
print(type(datas))
# find_one() 只返回一条查询结果,并且返回的是dict类型的数据
data = collection.find_one({"gender" : True})
print(data)
print(type(data))
# ***************** 修改数据 *****************
# update_one()只修改第一条符合条件的数据
collection.update_one({"age" : {"$gt" : 20}}, {"$set" : {"time" : datetime.now()}})
#update_many():修改多条符合条件的数据
collection.update_many({"age" : {"$gt" : 20}}, {"$set" : {"time" : datetime.now()}})
# update():只修改第一条符合条件的数据,同update_one()
collection.update({"age" : {"$gt" : 20}}, {"$set" : {"time" : "2018-11-01"}})
# ***************** 插入数据 *****************
# insert_one():只能插入一条数据
insert_data = {"_id" : 8, "name" : "小八", "like" : "鱼", "age" : 24, "gender" : True}
collection.insert_one(insert_data)
# insert_many():可以插入多条数据
insert_datas = [
{
"_id" : 9,
"name" : "批量甲",
"like" : "鹦鹉",
"age" : 31,
"gender" : False
},
{
"_id" : 10,
"name" : "批量乙",
"like" : "蛇",
"age" : 24,
"gender" : True
},
{
"_id" : 11,
"name" : "批量丙",
"like" : "枪",
"age" : 15,
"gender" : False
},
{
"_id" : 12,
"name" : "批量丁",
"like" : "food",
"age" : 47,
"gender" : True
},
{
"_id" : 13,
"name" : "批量酉",
"like" : "ball",
"age" : 34,
"gender" : False
}
]
collection.insert_many(insert_datas)
# insert():可以插入一条或多条数据,数据类型可以是dict和list
insert_datas = [
{
"_id" : 14,
"name" : "批量a",
"like" : "书法",
"age" : 31,
"gender" : False
},
{
"_id" : 15,
"name" : "批量b",
"like" : "编程",
"age" : 24,
"gender" : True
},
{
"_id" : 16,
"name" : "批量c",
"like" : "游泳",
"age" : 15,
"gender" : False
}
]
collection.insert({"_id" : 17, "name" : "八王爷", "like" : "鱼", "age" : 24, "gender" : True})
# ***************** 删除数据 *****************
# delete_one():只能删除一条数据
collection.delete_one({"age" : 15})
# delete_many():删除多条数据
collection.delete_many({"name" : {"$regex" : "批量"}})