当前位置:网站首页>MongoDB的一些基础用法
MongoDB的一些基础用法
2022-06-25 22:12:00 【大魔王的日常Log】
创建MongoDB容器
docker run di --name=tianxingwen_mongodb -p 27017:27017 mongo
远程登录
mongo 192.168.53.132
创建数据库
use spitdb
增删改查语法
查询
语法:
db.集合名称.find()
db.spit.find()
根据id查询某条数据
db.spit.find({"_id":“1”})
db.spit.findOne({"_id":“1”})
查询前几条数据
db.spit.find().limit(3)
新增
插入数据
语法:
db.集合名称.insert(数据);
db.spit.insert({content:“好冷啊”,visits:10})
修改
语法:
db.集合名称.update(条件,修改后的数据)
db.spit.update({_id:“1”},{visits:NumberInt(1000)})
注意上面这条命令执行后id为1的其他字段都会自动删掉
解决办法:
db.spit.update({_id:“2”},{$set:{visits:NumberInt(2000)}})
删除
语法:
db.集合名称.remove(条件)
删除全部
db.spit.remove({})
根据条件删除 删除visits:1000的这条数据
db.spit.remove({visits:1000})
统计
统计所有
db.spit.count()
根据指定条件统计
db.spit.count({userid:“1013”})
模糊查询
查询content字段包含java的所有文档
db.spit.find({content:/java/})
查询content以java开头的
db.spit.find({content:/^java/})
大于小于
语法:
db.集合名称.find({ “field” : { $gt: value }}) // 大于: field > value db.集合名称.find({ “field” : { $lt: value }}) // 小于: field < value db.集合名称.find({ “field” : { $gte: value }}) // 大于等于: field >= value
db.集合名称.find({ “field” : { $lte: value }}) // 小于等于: field <= value
db.集合名称.find({ “field” : { $ne: value }}) // 不等于: field != value
查询id>1的数据
db.spit.find({"_id":{$gt:“1”}})
包含与不包含
查询userid字段包含1013,1014的文档
db.spit.find({userid:{$in:[“1013”,“1014”]}})
查询userid字段不包含1013,1014的文档
db.spit.find({userid:{$nin:[“1013”,“1014”]}})
条件连接
语法格式
$and:[ { },{ },{ } ]
查询集合中visits大于等于1000 并且小于2000的文
db.spit.find({ KaTeX parse error: Expected '}', got 'EOF' at end of input: and:[ {visits:{ gte:1000}} ,{visits:{$lt:2000} }]})
如果两个以上条件之间是或者的关系,我们使用 操作符进行关联,与前面and的使用 方式相同
KaTeX parse error: Expected '}', got 'EOF' at end of input: …*db.spit.find({ or:[ {userid:“1013”} ,{visits:{$lt:2000} }]})
二级标题
对某列值在原有值的基础上进行增加或减少,可以使用KaTeX parse error: Expected '}', got 'EOF' at end of input: …ate({_id:"2"},{ inc:{visits:NumberInt(1)}} )**
边栏推荐
- Raspberry pie sends hotspot for remote login
- 猕猴桃酵素的功效_过路老熊_新浪博客
- JS中的原型链面试题--Foo和 getName
- Circuit de fabrication manuelle d'un port série de niveau USB à TTL pour PL - 2303hx Old bear passing Sina blog
- Network protocol: detailed explanation of redis protocol
- 网络连接验证
- Unsigned and signed vernacular
- Literature research (IV): Hourly building power consumption prediction based on case-based reasoning, Ann and PCA
- Installation of third-party library iGraph for social network visualization
- Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
猜你喜欢
Literature research (IV): Hourly building power consumption prediction based on case-based reasoning, Ann and PCA
懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客
Bit Compressor [蓝桥杯题目训练]
Building cloud computers with FRP
我的博客今天2岁167天了,我领取了先锋博主徽章_过路老熊_新浪博客
Explain in detail the three types of local variables, global variables and static variables
【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)
Unsigned and signed vernacular
数组常用的一些操作方法
随机推荐
P3052 [USACO12MAR]Cows in a Skyscraper G
[reprint]rslogix 5000 instance tutorial
Redis之常见问题
Bit Compressor [蓝桥杯题目训练]
寻找翻转数组的最小值[抽象二分]
10.2.2、Kylin_kylin的安装,上传解压,验证环境变量,启动,访问
Search rotation array ii[Abstract dichotomy exercise]
Using swiper to realize the rotation chart
Topic36——53. 最大子数组和
Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
Record some CF questions
Talk about the copy on write mechanism of PHP variables or parameters
Tensorflow中CSV文件数据读取
Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
Topic36——53. Maximum subarray and
About the swoole coroutine container
huibian
C ++ 引用与指针总结
文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测
Reading notes on how to connect the network - hubs, routers and routers (III)