首页 > 数据库 >MongoDB的使用

MongoDB的使用

时间:2023-04-27 14:14:19浏览次数:37  
标签:sname MongoDB db fib 李四 使用 teacher

 进入MongoDB

因为版本是6.0,所以需要在终端输入mongosh,该命令相当于6.0版本之前的mongo命令

root@Mongodb:~# mongosh
Current Mongosh Log ID: 63f48e2e5d50ed0f2ed35d3c
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.7.1
Using MongoDB: 6.0.4
Using Mongosh: 1.7.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
The server generated these startup warnings when booting
2023-02-21T16:54:50.226+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2023-02-21T16:54:50.700+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-02-21T16:54:50.700+08:00: vm.max_map_count is too low
------

------
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------
test>

4.2 查看MongoDB数据库中的所有数据库

test> show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB

4.3 打开或者新建一个数据库

MongoDB不需要预先创建文档,在使用时自动创建

test> use fib
switched to db fib
fib>

4.4 添加集合

集合相当于mysql数据库中的表

fib> db.createCollection('teacher')
{ ok: 1 }
fib> show collections
teacher

4.5 插入数据

fib> db.teacher.insert({_id:1,sname:'张三',sage:20})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{ acknowledged: true, insertedIds: { '0': 1 } }

4.6 查询所有记录

fib> db.teacher.find()
[ { _id: 1, sname: '张三', sage: 20 } ]

4.7 更新操作

fib> db.teacher.update({_id:1},{$set:{sname:'李四'}})
DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}

4.8 查询

4.8.1 查询sname='李四'的记录

fib> db.teacher.find({sname:'李四'})
[ { _id: 1, sname: '李四', sage: 20 } ]

4.8.2 查询指定列sname数据

fib> db.teacher.find({},{sname:1})
[ { _id: 1, sname: '李四' } ]

4.8.3 AND条件查询

fib> db.teacher.find({sname:'李四',sage:20})
[ { _id: 1, sname: '李四', sage: 20 } ]

4.8.4 OR条件查询

fib> db.teacher.find({$or:[{sage:20},{sage:21}]})
[ { _id: 1, sname: '李四', sage: 20 } ]

4.8.5 格式化输出

fib> db.teacher.find().pretty()
[ { _id: 1, sname: '李四', sage: 20 } ]

4.9 删除

4.9.1 删除数据

fib> db.teacher.remove({sname:'李四'})
DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite.
{ acknowledged: true, deletedCount: 1 }

4.9.2 删除集合

fib> db.teacher.drop()
true
———————————————— 

 

 

相当于我们平常输入mongo的时候,默认连接的是本地的mongo且端口是27017的。当然,使用命令行连接的方式肯定还有其他很多可选参数,大家有需要的可以自己去官网查查资料研究研究。

2

使用mongosh命令

安装后,默认在bin目录下是没有mongosh的命令的:

 

这个时候可以去官网下一个MongoDB Shell,下载地址:

https://www.mongodb.com/try/download/shell

下载完之后,建议安装到bin目录下 ,这样环境变量就不用再重新去额外配置了,安装完之后,我们再看一下bin目录下的内容:

 安装好之后,连接本地默认的数据库的话,也可以使用mongosh的命令直接连接:

 要连接其他服务器的数据库时,可以使用下面的命令:

mongosh "mongodb://ip:port"
mongosh ip:port/数据库 -u 用户名 -p 密码
mongosh -u 用户名 -p 密码 --port 端口号 --host ip 数据库名

 

标签:sname,MongoDB,db,fib,李四,使用,teacher
From: https://www.cnblogs.com/chuangsi/p/17358734.html

相关文章

  • mongoDB-mongosh
    作者:husky_1链接:https://www.jianshu.com/p/cba102845927来源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。mongoDBShell(mongosh)是一个功能齐全的JavaScript和Node.js14.xREPL环境,用于与MongoDB数据库进行交互。我们通过使用MongoDBSh......
  • 使用easyocr识别图片文字(本地图片和网络图片)
    fromPILimportImageimportrequestsfromioimportBytesIOimporteasyocrimportnumpyasnpfrompathlibimportPathfile_url=r'./img/1.jpg'#需识别的图片file_url=r"****7b00ea.jpg"split_symbol=''#默认空格为分隔......
  • 在Ubuntu22.04/16.04中安装MongoDB6.0
    一、MongoDB简介MongoDB是一个基于分布式文件存储的数据库,采用C++语言编写,旨在为Web应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是目前非关系数据库当中功能最丰富、最像关系数据库的数据库。传统的关系数据库一般由数据库(da......
  • HP-UX下du命令使用技巧
    本人习惯了Linux环境的du命令,在HP-UX下,发现du命令真的非常难用,有种让人很难受的感觉。主要是因为HP-UX下的du命令参数比Linux平台du命令参数要少很多,尤其是没有-h这个参数,它只能以kb形式显示文件/文件夹的大小,对于我来说,看起来非常不直观。下面是工作中,HP-UX平台使用du命令的一些......
  • kivy中按钮组件的所有方法使用和参数说明
    __init__(self,**kwargs):**kwargs:其他未指定参数的关键字参数。on_press(self):无参数。on_release(self):无参数。on_state(self,widget,value):widget:触发状态改变的按钮组件。value:按钮组件的新状态值。set_disabled(self,value):value:布尔值,表示按钮......
  • 使用 youth5201314:banner 库时出现 ViewPager 或 xandroid 报错
    使用youth5201314:banner这个库的1.4.10版本开发时,Build时报错找不到android.support.v4.view.ViewPager的类文件找不到androidx.fragment.app.Fragment的类文件需要在Project的gradle.properties配置文件中加一行,用于自动迁移第三方库android.enableJetifier=......
  • Ubuntu22.04 安装 mysql8,redis7,MongoDB6
     服务器的准备我的服务器是在腾讯云租的,所以服务器的apt源都是默认配好的,没配好的自行网上查找apt源配置。本文同样适用于Ubuntu22,20。Ubuntu18亦可参考。云服务器一般防火墙未开放端口访问,请自行配置,否则后续远程访问不了:mysql:3306redis:6379MongoDB:27017更新所有......
  • MongoDB忘记密码、修改密码总结
    一、修改密码:切换至mongo的bin目录下,登录mongouseadmindb.changeUserPassword('用户名','新密码');db.auth('用户名','新密码');二、忘记密码,重置密码:步骤如下:1、找到mongodb的配置文件通过ps-ef|grepmongod找到mongodb的配置文件mongod.conf也可以通过find/-na......
  • 使用ethtool排查网卡速率问题
    今天去现场帮一个客户排查备份网络速率问题。用户期望是万兆的速率,但实际上目前只有千兆,因为目前上面运行着数据库,且数据量较大,千兆的备份网络速率不能满足用户备份数据库的时长要求。首先,确认备份网络是由两块网卡(eth3,eth4)做了bonding,起名为bondeth1。使用ethtool查看底层的et......
  • 使用Pandas实现1-6列分别和第0列比大小得较小值
    今日鸡汤还作江南会,翻疑梦里逢。大家好,我是皮皮。一、前言前几天在Python白银交流群【星辰】问了一个pandas处理Excel数据的问题,提问截图如下:下图是他的原始代码截图:二、实现过程其实他这个代码,已经算实现了,如果分别进行定义的话,每一列做一个变量接收,也是可以实现效果的,速度上虽......