当前位置:网站首页>MongoDB-CUD without R
MongoDB-CUD without R
2022-07-30 07:43:00 【HDLaZy】
1:基本语法
显示数据库
show dbs;
创建数据库/切换数据库
#It will not be displayed if the database is empty
use 数据名;
删除数据库
db.dropDatabase();
Displays collections in the database
show collections;
创建集合
db.createCollection('集合名');
删除集合
db.集合名.drop();
2:插入数据
插入单条数据
如果没有_id字段will generate one by default_id字段_id的value为唯一键,不能重复
db.集合名.insertOne(
{
"_id":1,
"name":"HDLaZy"
}
)
插入多条数据
Insert as an array
db.集合名.insertMany([
{
"name":"唐僧","age":"11"},
{
"name":"孙悟空","age":"1000","武器":"如意金箍棒"},
{
"name":"八戒","住址":"高老庄"}
])
结果:
Insert multiple or single data
db.集合名.insert()
save方法
save方法使用时,If the document has相同的_id,Then go directly to the document覆盖,如果没有相同的_id,那么就新增一条文档
db.集合名.save(
{
"_id":1,"name":"沙和尚"}
)
3:修改数据
覆盖修改
Overwrite the old data with the new data
db.集合名.update(
#参数1,类似于MySql的where
{
"name":"孙悟空"},
#参数2,类似于MySql的set XXX=YYY
{
"name":"弼马温","住址":"花果山"},
#参数3,是否批量修改,默认为falseThat is, no batch modification is performed
{multi:false}
)
结果:
set修改器
setManipulate fields that do not exist,则会自动创建
db.集合名.update(
{
"name":"弼马温"},
{
'$set':{
"name":"齐天大圣","坐骑":"筋斗云"}}
)
结果:
unset修改器
使用unsetto delete a field,删除字段时,只在乎key而不在乎value
db.集合名.update(
{
"name":"齐天大圣"},
{
'$unset':{
'坐骑':1}}
)
结果:
4:删除数据
删除文档
db.集合名.remove(
#The delete condition is similar toMySql的where
{
'_id':ObjectId("62d6c7da3cde2139380017c3")}
)
删除所有文档
db.集合名.remove({})
边栏推荐
猜你喜欢

Vineyard: 开源分布式内存数据管理框架

元宇宙与图扑国风的碰撞,科技与文化的虚实融合

Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)

Cas 80750-24-9,去硫代生物素 N-羟基琥珀酰亚胺,淡黄色固体

Unity 渲染流水线

Linux(centos7)下安装MySQL
测试开发工程师成长日记010 - Jenkins中的CI/CD/CT(持续集成构建/持续交付/持续测试)

Network Protocol 01 - Basic Concepts

为数字政府构建智能化网络安全管控体系

GAIA-IR: Parallelized Graph Query Engine on GraphScope
随机推荐
为数字政府构建智能化网络安全管控体系
kubernetes搭建SonarQube进行代码扫描
原力计划微服务实战|集中配置中心Config非对称加密与安全管理
Biotin-NHS LC(72040-63-2)生物素接头|站点特定探针
Vineyard: An open source distributed in-memory data management framework
arthas常用命令
基于 JupyterLab 插件在 GraphScope 中交互式构图
Application of graph computing in network security analysis
图扑数字孪生北京故宫,推进旅游业元宇宙进程
Network Protocol 04 - Physical and Data Link Layers
【Untitled】
How to create a shortcut without the "shortcut" suffix?
网络协议01 - 基础概念
从 Vertex 到 Subgraph 再到 PIE: 并行图计算编程模型概览
Biotin-NH2|CAS:111790-37-5(生物素-氨基)是一种生物素化化合物
Network Protocol 01 - Basic Concepts
OP 代币和不可转让的 NFT 致力于建立新的数字民主
用于标记蛋白质和抗体的Biotin-LC-Sulfo-NHS|CAS:191671-46-2
基于粒子(Points)模拟雨雪天气效果
2021-05-26