IT评测·应用市场-qidao123.com

标题: MongoDB下载与基本使用(mac图文详解) [打印本页]

作者: 兜兜零元    时间: 2024-9-5 10:50
标题: MongoDB下载与基本使用(mac图文详解)
目录
一、下载安装
1.1 官网下载mongoDB
1.1.1 mongoDB
1.1.2 MongoDB GUI
1.2 下载流程
二、基本使用
2.1 创建数据库和聚集
2.2 插入
2.3 查询
2.4 修改
2.5 删除
三、case
3.1 销售case
3.1.1 实操
3.1.2 全部指令汇总


背景:
个人训练用
一、下载安装

1.1 官网下载mongoDB

1.1.1 mongoDB

Download MongoDB Community Server | MongoDB
1.点进去什么好像可以自动给你选好,下载安装包的同学可以参考Mac 最新安装MongoDB数据库步骤+ 可视化软件MongoDB Compass - 简书
2.我电脑装好了brew,所以选择的终端实行指令下载,建议大家多看看指令安装过程中的提示,很详细

1.1.2 MongoDB GUI

可视化软件  ---> MongoDB Compass下载,下载链接
MongoDB Compass Download (GUI) | MongoDB
1.2 下载流程

1.下载


2.提示的很清楚,去网站创建账,然后把终端验证码贴到网页里,资助你创建Atlas databas


3.终端提示的指令举行毗连。毗连成功


1.下载  MongoDB Compass 待更新

点击

二、基本使用

2.1 创建数据库和聚集

1.指令:
  1. -- 切换到要使用的数据库(如果不存在则会自动创建)
  2. use yanhan_db
  3. -- 创建集合
  4. db.createCollection("test")
复制代码
2.实操:

2.2 插入

1.指令:
  1. -- 插入单个文档:
  2. db.yanhan_db.insertOne({name:"john", age: 21})
  3. -- 插入多个文档:(要用中括号包起来)
  4. db.yanhan_db.insertMany([{name:"miky", age: 18}, {name:"king", age: 19}])
复制代码
2.实操:
插入一条

插入多条

2.3 查询

1.指令
  1. -- 查询所有文档:
  2. db.yanhan_db.find()
  3. -- 查询满足条件的文档:gt是greater than
  4. db.yanhan_db.find({age:{$gt:18}})
复制代码
2.实操:
查询全部

条件查询:查询 yanhan_db 数据库中年事大于 18 岁的文档,符合条件的两个文档的详细信息。

2.4 修改

1.指令:
  1. -- 更新单个文档:
  2. db.yanhan_db.updateOne({name:"miky"},{$set:{age:100}})
  3. -- 更新多个文档:lt小于,$set 更新
  4. db.yanhan_db.updateMany({ age: { $lt: 35 } }, { $set: { status: "Active" } })
复制代码
2.实操:
更新一个文档,

更新了 yanhan_db 数据库中年事小于 35 岁的全部文档,将它们的 status 字段设置为 "Active"。

2.5 删除

1.指令:
  1. -- 删除单个文档,name值为'Sally'的记录:
  2. db.yanhan_db.deleteOne({ name: "miky" })
  3. -- 删除多个文档,status值为'Active'的记录:
  4. db.yanhan_db.deleteMany({ status: "Active" })
复制代码
2.实操:

三、case

3.1 销售case

3.1.1 实操

1.创建

2.插入数据

3.查询全部

4.聚合指令,计算每个客户的总销售额和订单数目
   指令表明:
    实操展示:

5.查找每种产物的均匀销售价格和销售数目
   指令表明:
    实操:

6.筛选特定日期范围内的销售订单并投影字段,从 sales 聚集中选择指定日期范围内的订单数据,然后计算每个订单项的销售总额,并保存 order_id、product、quantity 和 totalAmount 这些字段。
   指令表明:
    其他:
  
  实操

7.删除表
  1. db.sales.drop()
复制代码

3.1.2 全部指令汇总

  1. -- 创建 sales
  2. use sale
  3. -- 创建 sales 集合并插入示例文档
  4. db.sales.insertMany([
  5.   {
  6.     "order_id": 1001,
  7.     "product": "Laptop",
  8.     "quantity": 2,
  9.     "unit_price": 1200,
  10.     "customer": "Alice",
  11.     "order_date": ISODate("2024-06-07T08:30:00Z")
  12.   },
  13.   {
  14.     "order_id": 1002,
  15.     "product": "Monitor",
  16.     "quantity": 1,
  17.     "unit_price": 500,
  18.     "customer": "Bob",
  19.     "order_date": ISODate("2024-06-10T10:15:00Z")
  20.   },
  21.   {
  22.     "order_id": 1003,
  23.     "product": "Keyboard",
  24.     "quantity": 3,
  25.     "unit_price": 50,
  26.     "customer": "Alice",
  27.     "order_date": ISODate("2024-06-15T14:45:00Z")
  28.   },
  29.   {
  30.     "order_id": 1004,
  31.     "product": "Mouse",
  32.     "quantity": 5,
  33.     "unit_price": 20,
  34.     "customer": "Charlie",
  35.     "order_date": ISODate("2024-07-09T09:30:00Z")
  36.   }
  37. ])
  38. -- 查询sales全部
  39. db.sales.find()
  40. -- 计算每个客户的总销售额和订单数量
  41. db.sales.aggregate([
  42.   {
  43.     $group: {
  44.       _id: "$customer", // 按客户分组
  45.       totalSales: { $sum: { $multiply: ["$quantity", "$unit_price"] } }, // 计算总销售额,使用 $multiply 计算每个订单的销售额,然后用 $sum 计算每个客户的总销售额
  46.       totalOrders: { $sum: 1 } // 计算总订单数量,每次遇到一个订单就加一
  47.     }
  48.   },
  49.   { $sort: { totalSales: -1 } } // 根据 totalSales 字段进行降序排序,显示总销售额最高的客户在前面
  50. ])
  51. -- 查找每种产品的平均销售价格和销售数量
  52. db.sales.aggregate([
  53.   {
  54.     $group: {
  55.       _id: "$product",
  56.       avgPrice: { $avg: "$unit_price" },
  57.       totalQuantity: { $sum: "$quantity" }
  58.     }
  59.   },
  60.   { $sort: { _id: 1 } } // 按产品名称升序排序
  61. ])
  62. --筛选特定日期范围内的销售订单并投影字段
  63. db.sales.aggregate([
  64.   {
  65.     $match: {
  66.       order_date: {
  67.         $gte: ISODate("2024-06-01"),
  68.         $lte: ISODate("2024-06-30")
  69.       }
  70.     }
  71.   },
  72.   {
  73.     $project: {
  74.       order_id: 1,
  75.       product: 1,
  76.       quantity: 1,
  77.       totalAmount: { $multiply: ["$quantity", "$unit_price"] }
  78.     }
  79.   }
  80. ])
  81. -- 删除 sales 集合
  82. db.sales.drop()
复制代码

参考:MongoDB安装、基础操纵和聚合实例先容_MongoDB_脚本之家

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4