MongoDB是一个基于分布式文件存储 [1] 的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引
创建数据库
use articledb
创建集合
db.createCollection("my")
查询集合
show collections
删除集合
db.集合.drop()
db.my.drop()
集合的增删改查
1.单个插入,比如我这里向comment放入一条评论表数据
db.comment.insert({
"articleid":"10000","content":"今天天气真好啊,阳光明媚","userid":"1001",
"nickename":"rose","createdatetime":new Date(),
"likenum":NumberInt(10),"state":null
})
2.查询
db.comment.find()
3.插入多个 insertMany()
db.comment.insertMany([{
"articleid":"10001","content":"今天大雾,雾蒙蒙的","userid":"1002",
"nickename":"rose","createdatetime":new Date(),
"likenum":NumberInt(10),"state":null
},{
"articleid":"10002","content":"今天天气真冷,冻死宝宝了","userid":"1003",
"nickename":"rose","createdatetime":new Date(),
"likenum":NumberInt(10),"state":null
}])
4.条件查询
db.coment.find(),通过查询方法里面放入json格式的参数,进行条件查询,比如查询userid为1003的评论
db.comment.find({"userid":"1003"})
5.投影查询 ,如果只需要查出表部分字段
db.comment.find({"userid":"1003"},{userid:1,nickename:1,_id:0})
5 批量插入的时候并不会因为某一条的失败而回滚,所以需要tryCatch
文档的更新
文档的更新语法 db.collection.update(query,update,options)
覆盖的修改
db.comment.update({userid:"1001"},{likenum:NumberInt(100)})
执行后我们发现,这条文档除了likenum这个字段,其他的都不见了
局部修改
为了解决这个问题。我们需要使用修改器$set
db.comment.update({userid:"1002"},{$set:{likenum:NumberInt(100)}})
文档的删除
文档的删除语法
db.集合名称.remove(条件)
db.comment.remove({_id:ObjectId("6311b7305e41940620ddd71d")});
分页查询
查询总数
db.comment.count({userid:"1002"})
分页查询,mongdb提供一个skip()
db.comment.find().limit(2).skip(3);
排序查询
sort()方法对数据进行排序,sort方法,可以通过参数指定排序的字段,并使用1和-1指定升序和降序
db.comment.find().sort({likenum:-1})
比较查询
< ,<= ,>,>= 这些操作符也是很常用
db.集合名称.find({"field":{ $gt:value}}) //大于:field>value
db.集合名称.find({"field":{ $lt:value}}) //小于:field<value
db.集合名称.find({"field":{ $gte:value}}) //大于等于:field>=value
db.集合名称.find({"field":{ $lte:value}}) //小于:field<=value
db.集合名称.find({"field":{ $lte:value}}) //不等于:field !=value
例如查询评论数大于10的记录
db.comment.find({likenum:{$gt:NumberInt(10)}})
包含查询$in操作符
db.comment.find({userid:{$in:["1001","1002","1003"]}})
索引的使用
索引可以提升查询的效率,mongodb支持单字段的索引和复合索引
1.查看索引
db.collection.getIndexes()
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "articledb.comment"
}
v 表示字段的版本号码
创建索引
1.在集合上创建索引
语法
db.collection.createIndex(keys,options)
db.comment.createIndex({userid:1})
2.复合索引:对于userid和nickname 同时建立符合(Compound)索引
db.comment.createIndex({userid:1,nickname:-1})
索引的移除
db.comment.dropIndex({userid:1})
查看执行计划 调用explain()
db.comment.find({userid:"1003"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "articledb.comment",
"indexFilterSet" : false,
"parsedQuery" : {
"userid" : {
"$eq" : "1003"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"userid" : 1
},
"indexName" : "userid_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userid" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userid" : [ "[\"1003\", \"1003\"]" ]
}
}
},
"rejectedPlans" : [
{
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"userid" : 1,
"nickname" : -1
},
"indexName" : "userid_1_nickname_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"userid" : [ ],
"nickname" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"userid" : [ "[\"1003\", \"1003\"]" ],
"nickname" : [ "[MaxKey, MinKey]" ]
}
}
}
]
},
"serverInfo" : {
"host" : "appledeMacBook-Pro.local",
"port" : 27017,
"version" : "4.0.10",
"gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
},
"ok" : 1
}
从winningplan的stage为FETCH 就表示命中的索引