数据库

MongoDB

单词:

  • MongoDB:蒙古币
  • data:数据
  • base:基地、仓库,库
  • database:数据库
  • collection:收集,集合
    • 表(在MongoDB中)
  • document:文档
    • 一条记录(在MongoDB中)
  • insert:插入
  • product:产品
  • Connect:连接
  • compass:指南针

数据库的分类

  • Excel - 微软 微型
  • Access - 微软 小型
  • SQLServer - 微软 中型、大型 老牌
  • MySQL - 老牌
  • MarioDB
  • MongoDB - 后起之秀
  • Oracle - 大型数据库

MongoDB三大产品 + 工具包

  • atlas:云数据库
  • 企业版MongoDB
  • 社区办MongoDB
  • 工具包:
    • Compass(指南针):MongoDB的可视化交互软件
    • Shell:MongoDB的命令行交互软件
    • VS Code Plugin:MongoDB为VSCode提供的数据库交互插件
    • APP交互API

Atlas云数据库

  1. 添加IP地址
  2. 添加数据库用户
  3. 创建数据库
    • 选择香港服务器
    • 选择连接方式(从本地电脑连接云数据库)
    • 创建用户名
    • 添加IP地址

本地连接云数据库

  • vs code插件连接
  • compasske可视化交互软件
  • mongosh命令行交互软件
  • 编程语言接口连接

连接字符串

vs code连接字符串

格式:

协议://<用户名>:<密码>@数据库的地址 / 数据库名

修改用户名和密码

mongodb+srv://<username>:<password>@tian588.dzj51ex.mongodb.net/test

举例:连接本地数据库

mongodb://127.0.0.1/test

compass管理工具

官网推出的数据库可视化管理工具

特点:可视化

mongosh管理工具

官网推出的数据库非可视化(命令行)管理工具

特点:不可视,使用命令交互

安装mongosh

本地安装MongoDB

总结

  1. 请说出三种常见数据库
    • SQLServer
    • mySQL
    • MongoDB
  2. MongoDB的三大产品
    • 云数据库
    • 企业版
    • 社区办(免费、供学习使用)
  3. 数据库管理工具
    • 指南针
    • mongosh
    • vscede 插件
  4. 本地连接远程云数据库时连接字符串的格式写出来
    1. 要学会创建库、连接库

MongoDB数据的启动和停止

window系统:服务 => MongoDB => 邮件启动或停止

Linux系统:

mongosh

mongosh是什么?

  • 是一个管理MongoDB的shell
  • 是一个与mongodb数据库交互的界面

shell是什么

shell是一个交互界面。

连库和退库

使用mongosh连接本地数据库

第一步:确认数据库已启动

第二步:使用连接字符串连接

mongosh "mongodb://127.0.0.1/test"

退出mongosh的连接

exit

查询数据库

show dbs

引用当前库

db

切换数据库

use

空数据库是不显示的

空数据库是一个没有物理地址的数据库名。

删除

db.dropDatabase()

表操作

显式创建表

db.createCollection('comments')

查询表

show collections
Last login: Mon Mar 13 19:12:18 on ttys004
➜ ~ mongosh "mongodb://127.0.0.1/test"
Current Mongosh Log ID: 640f0582e3bd2bb061a80f59
Connecting to: mongodb://127.0.0.1/test?
directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB: 6.0.4
Using Mongosh: 1.8.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
 The server generated these startup warnings when booting
2023-03-13T11:52:27.402+08:00: Access control is not enabled for the database. Read
and write access to data and configuration is unrestricted
2023-03-13T11:52:27.402+08:00: Soft rlimits for open file descriptors 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> show dbs //显示有⼏个数据库
admin 40.00 KiB
blogdb 40.00 KiB
config 72.00 KiB
local 72.00 KiB
myNewDB 40.00 KiB
test 24.00 KiB
test> db //引⽤当前库
test
test> use admin //切换数据库
switched to db admin
admin> use test//切换数据库
switched to db test
test> db.dropDatabase()//删除数据库
{ ok: 1, dropped: 'test' }
test> show dbs
admin 40.00 KiB
blogdb 40.00 KiB
config 96.00 KiB
local 72.00 KiB
myNewDB 40.00 KiB
test> use blogdb //切换数据库
switched to db blogdb
blogdb> db.dropDatabase() //删除数据库
{ ok: 1, dropped: 'blogdb' }
blogdb> show dbs //查询数据库
admin 40.00 KiB
config 96.00 KiB
local 72.00 KiB
myNewDB 40.00 KiB
blogdb> use test //切换数据库
switched to db test
test> use hello //切换数据库
switched to db hello
hello> use world //切换数据库
switched to db world
world> use test
switched to db test
test> db //引⽤数据库
test
test> db.users.insertOne({x:1}) //隐式创建users表
{
 acknowledged: true,
 insertedId: ObjectId("640f0a85e3bd2bb061a80f5a")
}
test> show dbs //显示数据库
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
local> db
local
local> show collections //显示有⼏个表
startup_log
local> use test
switched to db test
test> db.createCollection('users') //显式创建users表
{ ok: 1 }
test> db.createCollection('posts')//显式创建posts表
{ ok: 1 }
test> show collections//查询有⼏个表
comments
posts
test> db.users.insertOne({username:'zhangsan',age:19,phone:1331111111})////隐式创建users
表
{
 acknowledged: true,
 insertedId: ObjectId("640f0e42e3bd2bb061a80f5b")
}
test> show collections
comments
posts
users
test>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容