CRUD操作
创建数据库
use 数据库名称 use xc
查看当前正在使用的数据库命令
db
数据库的删除
db.dropDatabase()
创建一个名为 mycollection 的普通集合。
db.createCollection("mycollection")
查看当前库中的表:show tables命令
show collections 或者 show tables
集合的删除
db.collection.drop()或db. 集合名 .drop()
文档基本CRUD
向comment的集合(表)中插入一条数据
db . comment . insert ({"articleid" : "100000" ,"content" : " 今天天气真好,阳光媚" ,"userid" : "1001" ,"nickname" : "Rose" ,"createdatetime" : new Date (),"likenum" : NumberInt ( 10 ),"state" : null})
批量插入
db . comment . insertMany ([{"_id" : "1" , "articleid" : "100001" , "content" : " 我们不应该把清晨浪费在手机上,健康很重要,一杯温水幸福你我他。" , "userid" : "1002" , "nickname" : " 相忘于江湖 " , "createdatetime" : new Date ( "2019-08- 05T22:08:15.522Z" ), "likenum" : NumberInt ( 1000 ), "state" : "1" },{ "_id" : "2" , "articleid" : "100001" , "content" : " 我夏天空腹喝凉开水,冬天喝温开水 " , "userid" : "1005" , "nickname" : " 伊人憔悴" , "createdatetime" : new Date ( "2019-08-05T23:58:51.485Z" ), "likenum" : NumberInt ( 888 ), "state" : "1" }]);
文档的基本查询
查询所有
db . comment . find ()或db . comment . find ({})
查询userid为1003的记录
db.comment.find({userid:'1003'})
查询用户编号是1003的记录,但只最多返回符合条件的第一条记录:
db.comment.findOne({userid:'1003'})
投影查询
>db.comment.find({userid:"1003"},{userid:1,nickname:1}){ "_id" : "4", "userid" : "1003", "nickname" : " 凯撒 " }{ "_id" : "5", "userid" : "1003", "nickname" : " 凯撒 " }
>db.comment.find({userid:"1003"},{userid:1,nickname:1,_id:0}){ "userid" : "1003", "nickname" : " 凯撒 " }{ "userid" : "1003", "nickname" : " 凯撒 " }
>db.comment.find({},{userid:1,nickname:1})
文档的更新
覆盖的修改:想修改_id为1的记录,点赞量为1001
db.comment.update({_id:"1"},{likenum:NumberInt(1001)})
db . comment . update ({ _id : "2" },{ $set :{ likenum : NumberInt ( 889 )}})
// 默认只修改第一条数据db . comment . update ({ userid : "1003" },{ $set :{ nickname : " 凯撒 2" }})// 修改所有符合条件的数据db . comment . update ({ userid : "1003" },{ $set :{ nickname : " 凯撒大帝 " }},{ multi : true })
db . comment . update ({ _id : "3" },{ $inc :{ likenum : NumberInt ( 1 )}})
删除文档
删除文档的语法结构
db. 集合名称 .remove( 条件 )
将数据全部删除,请慎用
db.comment.remove({})
删除_id=1的记录
db.comment.remove({_id:"1"})
文档的分页查询
统计查询使用count()方法
db.collection.count(query, options)
统计comment集合的所有的记录数
db.comment.count()
按条件统计记录数
db.comment.count({userid:"1003"})
分页列表查询
使用limit()方法来读取指定数量的数据,使用skip()方法来跳过指定数量的数据
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
db.comment.find().limit(3)
db.comment.find().skip(3)
// 第一页db.comment.find().skip(0).limit(2)// 第二页db.comment.find().skip(2).limit(2)// 第三页db.comment.find().skip(4).limit(2)
排序查询
db.COLLECTION_NAME.find().sort({KEY:1})
或db. 集合名称 .find().sort( 排序方式 )
复杂查询
模糊查询
查询包含开始的数据
db.comment.find({content:/开水/})
比较查询
db. 集合名称 .find({ "field" : { $gt: value }}) // 大于 : field > valuedb. 集合名称 .find({ "field" : { $lt: value }}) // 小于 : field < valuedb. 集合名称 .find({ "field" : { $gte: value }}) // 大于等于 : field >= valuedb. 集合名称 .find({ "field" : { $lte: value }}) // 小于等于 : field <= valuedb. 集合名称 .find({ "field" : { $ne: value }}) // 不等于 : field != value
db.comment.find({likenum:{$gt:NumberInt(700)}})
包含查询
db.comment.find({userid:{$in:["1003","1004"]}})
db.comment.find({userid:{$nin:["1003","1004"]}})
条件连接查询
db.comment.find({$and:[{likenum:{$gte:NumberInt(700)}},{likenum:{$lt:NumberInt(2000)}}]})
db.comment.find({$or:[ {userid:"1003"} ,{likenum:{$lt:1000} }]})
索引
索引的查看
db.xc.getIndexes()
索引的创建
db.collection.createIndex(keys, options)
单字段索引
复合索引
根据索引名字删除
删除 comment 集合中 userid 字段上的升序索引
想知道,建立的索引是否有效,效果如何,都需要通过执行计划查看
db.collection.find(query,options).explain(options)
idea文章微服务模块搭建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>mongodb</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
spring:
#数据源配置
data:
mongodb:
# 主机地址
host: 127.0.0.1
# 数据库
database: xc
# 默认端口是27017
port: 27017
#帐号
# username: root
#密码
# password: 123456
#也可以使用uri连接
#uri: mongodb://192.168.40.134:27017/xc
#单机有认证的情况下,也使用字符串连接
# uri: mongodb://root:123456@180.76.159.126:27017/xc