MongoDB-CUD没有R

本文介绍了MongoDB的基础语法,包括创建数据库、集合,以及如何进行数据的增删改查操作。重点讲解了insertOne, insertMany, save, update, unset和remove等关键方法,适合初学者快速上手。
摘要由CSDN通过智能技术生成

1:基本语法

显示数据库

show dbs;

创建数据库/切换数据库

#如果数据库为空则不会显示
use 数据名;

删除数据库

db.dropDatabase();

展示数据库中的集合

show collections;

创建集合

db.createCollection('集合名');

删除集合

db.集合名.drop();

2:插入数据

插入单条数据
如果没有_id字段则会默认生成一个_id字段
_id的value为唯一键,不能重复

db.集合名.insertOne(
	{
		"_id":1,
		"name":"HDLaZy"
	}
)

插入多条数据
以数组的形式插入

db.集合名.insertMany([
{"name":"唐僧","age":"11"},
{"name":"孙悟空","age":"1000","武器":"如意金箍棒"},
{"name":"八戒","住址":"高老庄"}
])

结果:
在这里插入图片描述

插入多条或者单条数据

db.集合名.insert()

save方法
save方法使用时,如果文档内有相同的_id,那么直接将文档覆盖如果没有相同的_id,那么就新增一条文档

db.集合名.save(
{"_id":1,"name":"沙和尚"}
)

3:修改数据

覆盖修改
使用新的数据覆盖原来的数据

db.集合名.update(
#参数1,类似于MySql的where
{"name":"孙悟空"},
#参数2,类似于MySql的set XXX=YYY
{"name":"弼马温","住址":"花果山"},
#参数3,是否批量修改,默认为false即不进行批量修改
{multi:false}
)

结果:
在这里插入图片描述

set修改器
set操作不存在的字段,则会自动创建

db.集合名.update(
{"name":"弼马温"},
{'$set':{"name":"齐天大圣","坐骑":"筋斗云"}}
)

结果:
在这里插入图片描述unset修改器
使用unset为删除某个字段,删除字段时,只在乎key而不在乎value

db.集合名.update(
{"name":"齐天大圣"},
{'$unset':{'坐骑':1}}
)

结果:
在这里插入图片描述

4:删除数据

删除文档

db.集合名.remove(
#删除条件类似于MySql的where
{'_id':ObjectId("62d6c7da3cde2139380017c3")}
)

删除所有文档

db.集合名.remove({})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值