MongoDB_04_插入和查询
2024-09-03 12:10:59
案列需求:
存在文章评论的数据存放到MongoDB中,数据结构参考如下:
数据库:articledb
专栏文章评论 comment / / 字段名称 字段含义 字段类型 备注 _id ID Object或String Mongo的主键 articleid 文章ID String content 评论内容 String userid 评论人的ID String nickname 评论人的昵称 String createdatetime 评论的日期时间 Date likenum 点赞数 int32 replaynum 回复数 int32 state 状态 String 0:不可见 1:可见 parentid 上级ID String 如果为0表示文章的顶级评论 数据库的操作
选择和创建数据库的语法
use 数据库名字
-----------------------------------
> use articledb
查看有权限查看的所有数据库
show dbs 或者 show databases
注意:在MongoDB中,集合只有在内容插入后才会创建!!!
----------------------------------------------------------------
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use articledb
switched to db articledb
> show dbs #并没有看到新创建的articledb库,因为创建时是在内存中,还没有持久化到磁盘
admin 0.000GB
config 0.000GB
local 0.000GB
> db
articledb
查看当前正在使用的数据库
db
注意:数据库名可以使满足以下条件的任意UTF-8字符串
*不能是空字符串
*不应该含有空格 . $ / \ \0等字符
*应该全部小写
*最多64字符
数据库的删除--语法格式如下
db.dropDatabase() #主要用来删除已经持久化的数据库
-----------------------------------------------------------
> db
articledb
> db.dropDatabase()
{ "ok" : 1 }集合操作----集合就类似于mysql中的表
1.1:集合的显示创建(了解)
基本语法:db.createCollection(name)
-name:要创建的集合名称
-------------------------------------------------
创建一个名为mycollection的集合
db.createCollection("mycollection")查看当前的集合(数据表)和删除集合
show collections 或者 show tables
db.集合名.drop()
---------------------------------------------
> use articledb # 创建一个数据库
switched to db articledb
> db.createCollection("mycollection") #创建一个集合
{ "ok" : 1 }
> show tables #查看集合
mycollection
> db.mycollection.drop() # 删除集合
true
CURD--重点
文档(document)的数据结构和JSON基本一样。
所有存储在集合中的数据都是BSON格式。
I插入
(1) 单个文档的插入
使用insert()或save()方法向集合中插入文档,语法如下:
db.collection名.insert(
<document or array of document>, --要插入到集合中的文档或文档数据
{
writeConcern:<document>, --可选
ordered:<boolean> --可选
}
)
-----------------------------------------------------------------------------------------
向comment的集合中插入一条测试数据:
db.comment.insert({"articleid":"100000","content":"我爱你,憨憨1","userid":"1001",
"nickname":"hanhan01","createdatetime":new Date(),"likenum":NumberInt(520),"state":null}) 解释:
1)comment集合如果不存在,则隐式创建
2)mongo中的数字,默认是double类型,如果需要存整型,就要使用函数NumberInt(整形数字),否则取出来会有问题
3)插入当前日期使用new Date()
4)插入的数据没有指定_id,会自动创建
5)如果某一字段没值,可以指定为null,或者不写该字段 执行:
db.comment.insert({"articleid":"100000","content":"我爱你,憨憨1","userid":"1001",
... "nickname":"hanhan01","createdatetime":new Date(),"likenum":NumberInt(520),"state":null})
WriteResult({ "nInserted" : 1 })
- (2)批量插入
```python
db.collection名.insertMany(
[<document 1>,<document 2>,...]
{
writeConcern:<document>, --可选
ordered:<boolean> --可选
}
)
-----------------------------------------------------------------------------------
向comment插入多条数据:
db.comment.insertMany([
{"_id":"2","articleid":"100002","content":"我爱你,绩憨憨2","userid":"1002",
"nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(521),"state":null},
{"_id":"3","articleid":"100003","content":"我爱你,绩憨憨3","userid":"1003",
"nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(522),"state":null},
{"_id":"4","articleid":"100004","content":"我爱你,绩憨憨4","userid":"1004",
"nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(523),"state":null},
])
解释:
1插入时指定了_id,则主键就是该值
2如果某条数据插入失败,将会终止插入,但是已经插入成功的数据--不会回滚
3因为批量插入的时候由于数据较多,容易出现失败,因此,可以使用try catch进行异常捕捉处理
执行:
> db.comment.insertMany([
... {"_id":"2","articleid":"100002","content":"我爱你,绩憨憨2","userid":"1002",
... "nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(521),"state":null},
... {"_id":"3","articleid":"100003","content":"我爱你,绩憨憨3","userid":"1003",
... "nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(522),"state":null},
... {"_id":"4","articleid":"100004","content":"我爱你,绩憨憨4","userid":"1004",
... "nickname":"hanhan","createdatetime":new Date(),"likenum":NumberInt(523),"state":null},
...
... ])
{ "acknowledged" : true, "insertedIds" : [ "2", "3", "4" ] } #成功
```
## II查询
- (1)基本的查询语法
```python
db.collection名.find(<query>,[projection])
---------------------------------------------
1:查询comment所有的内容
db.comment.find() 或者db.comment.find({})
执行:
> db.comment.find({})
{ "_id" : ObjectId("5e57cc7d24335849dc4a2ab8"), "articleid" : "100000", "content" : "我爱你,中国", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2020-02-27T14:04:45.459Z"), "likenum" : 10, "state" : null }
{ "_id" : "2", "articleid" : "100002", "content" : "我爱你,绩憨憨2", "userid" : "1002", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.708Z"), "likenum" : 10, "state" : null }
{ "_id" : "3", "articleid" : "100003", "content" : "我爱你,绩憨憨3", "userid" : "1003", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.709Z"), "likenum" : 10, "state" : null }
{ "_id" : "4", "articleid" : "100004", "content" : "我爱你,绩憨憨4", "userid" : "1004", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.709Z"), "likenum" : 10, "state" : null }
2:条件查询,查询userid为1003的记录
db.comment.find({"userid":"1003"})
执行:
> db.comment.find({"userid":"1003"})
{ "_id" : "3", "articleid" : "100003", "content" : "我爱你,绩憨憨3", "userid" : "1003", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.709Z"), "likenum" : 10, "state" : null }
3:查询多条数据,但是只想查到第一条
db.comment.find({'likenum':10})
执行:> db.comment.find({'likenum':10})
{ "_id" : ObjectId("5e57cc7d24335849dc4a2ab8"), "articleid" : "100000", "content" : "我爱你,中国", "userid" : "1001", "nickname" : "Rose", "createdatetime" : ISODate("2020-02-27T14:04:45.459Z"), "likenum" : 10, "state" : null }
{ "_id" : "2", "articleid" : "100002", "content" : "我爱你,绩憨憨2", "userid" : "1002", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.708Z"), "likenum" : 10, "state" : null }
{ "_id" : "3", "articleid" : "100003", "content" : "我爱你,绩憨憨3", "userid" : "1003", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.709Z"), "likenum" : 10, "state" : null }
{ "_id" : "4", "articleid" : "100004", "content" : "我爱你,绩憨憨4", "userid" : "1004", "nickname" : "hanhan", "createdatetime" : ISODate("2020-02-27T14:17:24.709Z"), "likenum" : 10, "state" : null }
只想查询到第一条:db.集合名.findOne({})
db.comment.findOne({'likenum':10})
执行:
> db.comment.findOne({'likenum':10})
{
"_id" : ObjectId("5e57cc7d24335849dc4a2ab8"),
"articleid" : "100000",
"content" : "我爱你,中国",
"userid" : "1001",
"nickname" : "Rose",
"createdatetime" : ISODate("2020-02-27T14:04:45.459Z"),
"likenum" : 10,
"state" : null
}
投影查询(Projection Query)
如果要查询结果返回部分字段,则需要使用投影查询
注意使用1:显示 0:不显示
1:查询结果只显示_id,userid,nickname
db.comment.find({'likenum':10},{userid:1,nickname:1}) 执行:
> db.comment.find({'likenum':10},{userid:1,nickname:1})
{ "_id" : ObjectId("5e57cc7d24335849dc4a2ab8"), "userid" : "1001", "nickname" : "Rose" }
{ "_id" : "2", "userid" : "1002", "nickname" : "hanhan" }
{ "_id" : "3", "userid" : "1003", "nickname" : "hanhan" }
{ "_id" : "4", "userid" : "1004", "nickname" : "hanhan" } 2:如果想去除_id,
db.comment.find({'likenum':10},{userid:1,nickname:1,_id:0}) 执行:
> db.comment.find({'likenum':10},{userid:1,nickname:1,_id:0})
{ "userid" : "1001", "nickname" : "Rose" }
{ "userid" : "1002", "nickname" : "hanhan" }
{ "userid" : "1003", "nickname" : "hanhan" }
{ "userid" : "1004", "nickname" : "hanhan" }
最新文章
- angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构
- 【有人@我】Android中高亮变色显示文本中的关键字
- IOS开发UI基础UITextFidle相关属性
- 流媒体学习一-------mediastreamer2 的简介
- javaScirpt学习之事件
- [原创] JavaScript 图片放大镜插件 enlarge.js 以及移动版 enlarge.touch.js
- CC150 上面重要的题目总结
- (java)从零开始之--异常处理(以文件拷贝为例)
- mysql 二进制安装的基本步骤
- 【转载】【转自AekdyCoin的组合数取模】
- C++学习之使用new的注意事项
- HighChart学习-更新数据data Series与重绘
- 【转】Android折叠效果实现案例
- 流API--缩减操作
- .net mvc + layui做图片上传(二)—— 使用流上传和下载图片
- zoomeye搜索+用selenium实现对佳能打印机的爬虫
- Python元组(tuple)
- java的poi 读取exc 文件
- Java循环中try...finally...遇到continue
- 使用SpringBoot入门案例
热门文章
- JQuery - $(this) 加 siblings() 的使用
- Uart学习笔记
- windows 批量杀进程 类似pkill
- 125、Java面向对象之引用传递实例三,int类型按值传递
- 一个基础又很重要的知识点:JDBC原理(基本案例和面试知识点)
- Decimal为SQL Server、MySql等数据库的一种数据类型
- C. Maximum Median 二分
- day10-Python运维开发基础(函数嵌套、nonlocal声明局部变量、闭包、locals/globals、lambda表达式)
- 移动互联网APP测试流程及测试点
- Linux centosVMware su命令、sudo命令、限制root远程登录