09-MongoDB的安装与简单使用

第9讲 MongoDB的安装与简单使用

整体课程知识点查看 :https://blog.csdn.net/j1451284189/article/details/128713764

本讲总结

MongoDB的安装

MongoDB的简单使用

一、MongoDB的安装

非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储形式类似 JSON 对象,它的字段值可以包含其他文档、数组及文档数组

1、安装与启动

安装教程:https://zhuanlan.zhihu.com/p/596465307
下载地址:https://www.mongodb.com/try/download/community
使用文档:https://www.mongodb.org.cn/tutorial/55.html

win10:
MongoDB:6.0.3
Mongosh: 1.6.2

win7 
安装教程:https://blog.csdn.net/qq_27093465/article/details/54574948

mongod --config  D:\LenovoSoftstore\mongo\mongo.conf --install --serviceName "MongoDB"

2、启动

net start MongoDB  #启动  需管理员权限(服务端)
net stop MongoDB #关闭数据库

mongosh # 启动客户端  6.0.3
mongo  # 启动客户端  3.4.24

#创建用户
#使用用户权限

exit()  #退出

show dbs # 查看数据库
use Mydata  #创建并进入数据库
db.Mydata.insert({"name":"google"})  #数据库插入数据
db.Mydata.find()
db.dropDatabase() #进入数据库后 删除

# 集合操作
show collections # 查看集合
db.createCollection("MyCollection")   # 创建MyCollectionj集合
db.MyCollection.insert({'username':'jenny','age':20,'salary':300}); #向集合中插入一些数据
db.MyCollection.find() #查询集合数据
db.MyCollection.drop() #删除集合

# MongoBulkWriteError: db already exists with different case already have: BulkWriteResult  暂未找到解决方法

二、MongoDB简单使用

# 数据
db.MyCollection.insert({'id':1,'name':'apple','age':20,'salary':true});
db.MyCollection.insert({'id':2,'name':'banana','age':20,'salary':false});
db.MyCollection.insert({'id':3,'name':'city','age':16,'salary':false});
db.MyCollection.insert({'id':4,'name':'delay','age':20,'salary':true});
db.MyCollection.insert({'id':5,'name':'effect','age':17,'salary':true});
db.MyCollection.insert({'id':6,'name':'finite','age':20,'salary':true});
db.MyCollection.insert({'id':7,'name':'guess','age':10,'salary':true});


# 增
db.MyCollection.insert({'username':'jenny','age':20,'salary':300}); 
dict_data = {
    'id':1,
    'name':'jenny',
    'age':55,
    'gender':true
}
db.MyCollection.insert(dict_data)

# 删
db.MyCollection.remove({'username':'jenny'}) #无效方法
db.MyCollection.deleteOne({'username':'zhangyu'}) #删一个
db.MyCollection.deleteMany({'gender':true}) #删多个
db.MyCollection.remove({}) #删所有数据

# 改
db.MyCollection.update({}); #无效方法
db.MyCollection.updateOne({id:7}, {$set: {name:'张三'}});
db.MyCollection.updateMany({gender:true}, {$set: {age:66}});
db.MyCollection.updateOne({id:1}, {$unset: {name:''}}); #去除某个key

# 查 简单
db.MyCollection.find() #查找所有
db.MyCollection.find({salary:false})
db.MyCollection.findOne({salary:false}) #查找一个

# 查 比较运算 $lt $lte $gt $gte $ne
db.MyCollection.find({age:{$lte:17}})
db.MyCollection.find({age:{$ne:20}})

# 查 逻辑运算 $and $or   注意大括号
db.MyCollection.find({age:{$gt:17},salary:true}) #多条件 默认and
db.MyCollection.find({$and:[{age:{$lte:17}},{name:'guess'}]})
db.MyCollection.find({$or:[{age:{$lte:17}},{name:'guess'}]})
db.MyCollection.find({$or:[{age:{$lte:17},salary:true},{name:'guess'}]})# 混用

#查 范围运算 $in $nin
db.MyCollection.find({age:{$in:[10, 20]}})
db.MyCollection.find({age:{$nin:[10, 20]}})

#查 正则
db.MyCollection.find({name:{$regex:'^b'}})
db.MyCollection.find({name:{$regex:'^B',$options:'i'}}) # 忽略大小写
db.MyCollection.find({name:/^b/})
db.MyCollection.find({name:/^b/i}) # 忽略大小写

# 查 定义函数 js代码
db.MyCollection.find({
    $where:function (){
        return this.age<20
    }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值