当前位置:网站首页>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({})
边栏推荐
猜你喜欢

Biotin-NH2|CAS:111790-37-5(生物素-氨基)是一种生物素化化合物

Test Development Engineer Growth Diary 017 - The Life Cycle of a Bug

Mastering JESD204B (1) – Debugging of AD6676

02-Cycript的使用

kubernetes搭建SonarQube进行代码扫描

GNNLab: A Novel GNN System Based on Spatial Sharing Ideas

Event Delivery and Responder Chains

Desthiobiotin-PEG4-Acid|脱硫生物素-PEG4-酸| 供应商和制造商

DADPS-生物素-炔基_CAS:2241685-22-1试剂反应原理

SQL并列排序问题
随机推荐
Unable to open socket file: target process not responding or HotSpot VM not loaded
多线程进阶(CountDownLatch,死锁,线程安全集合类)
libgrape-lite: 提供 GraphScope 的图分析能力
I can't hide it, I want to expose the bad things about cloud native
【无标题】
The concept and testing method of black box testing
测试开发工程师成长日记009 - 环境排排站:开发环境、测试环境、生产环境、UAT环境、仿真环境
D-Desthiobiotin|D-脱硫生物素|CAS:533-48-2用于蛋白质和细胞的标记
OP 代币和不可转让的 NFT 致力于建立新的数字民主
阿里京东“喜提”国电投,顶流互联网和能源大厂为何合作?
如何使用xilinx的FFT ip
为数字政府构建智能化网络安全管控体系
Jenkins一些常见问题
测试开发工程师成长日记017 - bug的生命周期
Unity 渲染流水线
元宇宙与图扑国风的碰撞,科技与文化的虚实融合
prometheus-tls加密
C#最优二叉树----哈夫曼树
替换xxx.jar的class文件命令
JSP自定义标签