当前位置:网站首页>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({})
边栏推荐
猜你喜欢
随机推荐
如何使用xilinx的FFT ip
SE_01
THREEJS导入外部OBJ+MTL后内存优化
Build an intelligent network security management and control system for digital government
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
How to create a shortcut without the "shortcut" suffix?
libgrape-lite: 提供 GraphScope 的图分析能力
测试开发工程师成长日记016 - 关于提测的那些事
Biotin-SS-NHS ester|生物素-二硫键-活性酯|CAS:122266-55-1具有良好的水溶性
PC DBCO-PEG3-Biotin|PC-生物素-PEG3-DBCO可用于使用无铜点击化学
软件测试_01
com.alibaba.datax.common.exception.DataXException: Code:[ESWriter-03]
多线程进阶(CountDownLatch,死锁,线程安全集合类)
Test Development Engineer Growth Diary 008 - Talking About Some Bugs/Use Case Management Platform/Collaboration Platform
2021-05-26
关于memcache内核,全网最通俗的讲解
大厂年薪50w+招聘具有测试平台开发能力的测试工程师
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
图扑软件数字孪生民航飞联网,构建智慧民航新业态
Mastering JESD204B (3) – Debugging of AD6676









