虽然mongodb这些年很流行,但笔者之前没研究过,现在有需求研究这类nosql的数据库,是为了验证其是否可被替换。
mongodb是很轻量的文档数据库,简单测试也懒得专门准备虚拟机环境了,直接在macos上安装测试下其基础功能。
- 1.使用 homebrew 安装 mongodb
- 2.启动/停止 mongodb 服务
- 3.启动 mongodb shell
- 4.体验 mongodb 基本操作
- 5.体验 mongodb 聚合操作
1. 使用 homebrew 安装 mongodb
# 添加 mongodb 存储库 brew tap mongodb/brew # 安装 mongodb 社区版 brew install mongodb-community
2. 启动/停止 mongodb 服务
# 启动 mongodb 服务 brew services start mongodb/brew/mongodb-community # 停止 mongodb 服务(这个当然要等我们体验测试完成后才停..) brew services stop mongodb/brew/mongodb-community
3. 启动 mongodb shell
# 打开 mongodb 的交互式 shell mongosh
在mongosh登录到mongodb时可以看到,笔者这里安装的是7.0.12版本的mongodb,使用默认端口27017:
jingyuzhao@jingyuzhao-mac ~ % mongosh current mongosh log id: 668ce3d3012a1d349d3a46b3 connecting to: mongodb://127.0.0.1:27017/?directconnection=true&serverselectiontimeoutms=2000&appname=mongosh+2.2.10 using mongodb: 7.0.12 using mongosh: 2.2.10 for mongosh info see: https://docs.mongodb.com/mongodb-shell/ ------ the server generated these startup warnings when booting 2024-07-09t15:09:54.021+08:00: access control is not enabled for the database. read and write access to data and configuration is unrestricted ------ test>
4. 体验mongodb基本操作
下面进行一些基本的 mongodb 操作示例:
1)创建数据库和集合:
-- 切换到要使用的数据库(如果不存在则会自动创建)
use mydb
-- 创建集合
db.createcollection("mycollection")2)插入文档:
-- 插入单个文档:
db.mycollection.insertone({ name: "alfred", age: 34 })
-- 插入多个文档:
db.mycollection.insertmany([
{ name: "mcdull", age: 33 },
{ name: "sally", age: 4 }
])3)查询文档:
-- 查询所有文档:
db.mycollection.find()
-- 查询满足条件的文档:
db.mycollection.find({ age: { $gt: 25 } })4)更新文档:
-- 更新单个文档:
db.mycollection.updateone({ name: "alfred" }, { $set: { age: 29 } })
-- 更新多个文档:
db.mycollection.updatemany({ age: { $lt: 35 } }, { $set: { status: "active" } })5)删除文档:
-- 删除单个文档,name值为'sally'的记录:
db.mycollection.deleteone({ name: "sally" })
-- 删除多个文档,status值为'active'的记录:
db.mycollection.deletemany({ status: "active" })5. 体验mongodb聚合操作
1)创建测试用例
-- 删除 sales 集合
db.sales.drop()
-- 创建 sales 集合并插入示例文档
db.sales.insertmany([
{
"order_id": 1001,
"product": "laptop",
"quantity": 2,
"unit_price": 1200,
"customer": "alice",
"order_date": isodate("2024-06-07t08:30:00z")
},
{
"order_id": 1002,
"product": "monitor",
"quantity": 1,
"unit_price": 500,
"customer": "bob",
"order_date": isodate("2024-06-10t10:15:00z")
},
{
"order_id": 1003,
"product": "keyboard",
"quantity": 3,
"unit_price": 50,
"customer": "alice",
"order_date": isodate("2024-06-15t14:45:00z")
},
{
"order_id": 1004,
"product": "mouse",
"quantity": 5,
"unit_price": 20,
"customer": "charlie",
"order_date": isodate("2024-07-09t09:30:00z")
}
])查询这个集合结果:
db.sales.find()
mydb> db.sales.find()
[
{
_id: objectid('668cf766749a72317b175646'),
order_id: 1001,
product: 'laptop',
quantity: 2,
unit_price: 1200,
customer: 'alice',
order_date: isodate('2024-06-07t08:30:00.000z')
},
{
_id: objectid('668cf766749a72317b175647'),
order_id: 1002,
product: 'monitor',
quantity: 1,
unit_price: 500,
customer: 'bob',
order_date: isodate('2024-06-10t10:15:00.000z')
},
{
_id: objectid('668cf766749a72317b175648'),
order_id: 1003,
product: 'keyboard',
quantity: 3,
unit_price: 50,
customer: 'alice',
order_date: isodate('2024-06-15t14:45:00.000z')
},
{
_id: objectid('668cf766749a72317b175649'),
order_id: 1004,
product: 'mouse',
quantity: 5,
unit_price: 20,
customer: 'charlie',
order_date: isodate('2024-07-09t09:30:00.000z')
}
]
mydb> 2)执行聚合操作
示例 1: 计算每个客户的总销售额和订单数量
db.sales.aggregate([
{
$group: {
_id: "$customer",
totalsales: { $sum: { $multiply: ["$quantity", "$unit_price"] } },
totalorders: { $sum: 1 }
}
},
{ $sort: { totalsales: -1 } } // 按总销售额降序排序
])查询结果:【计算每个客户的总销售额和订单数量】
[
{ _id: 'alice', totalsales: 2550, totalorders: 2 },
{ _id: 'bob', totalsales: 500, totalorders: 1 },
{ _id: 'charlie', totalsales: 100, totalorders: 1 }
]示例 2: 查找每种产品的平均销售价格和销售数量
db.sales.aggregate([
{
$group: {
_id: "$product",
avgprice: { $avg: "$unit_price" },
totalquantity: { $sum: "$quantity" }
}
},
{ $sort: { _id: 1 } } // 按产品名称升序排序
])查询结果:【查找每种产品的平均销售价格和销售数量】
[
{ _id: 'keyboard', avgprice: 50, totalquantity: 3 },
{ _id: 'laptop', avgprice: 1200, totalquantity: 2 },
{ _id: 'monitor', avgprice: 500, totalquantity: 1 },
{ _id: 'mouse', avgprice: 20, totalquantity: 5 }
]示例 3: 筛选特定日期范围内的销售订单并投影字段
db.sales.aggregate([
{
$match: {
order_date: {
$gte: isodate("2024-06-01"),
$lte: isodate("2024-06-30")
}
}
},
{
$project: {
order_id: 1,
product: 1,
quantity: 1,
totalamount: { $multiply: ["$quantity", "$unit_price"] }
}
}
])查询结果:【筛选特定日期范围内的销售订单并投影字段】
[
{
_id: objectid('668cf766749a72317b175646'),
order_id: 1001,
product: 'laptop',
quantity: 2,
totalamount: 2400
},
{
_id: objectid('668cf766749a72317b175647'),
order_id: 1002,
product: 'monitor',
quantity: 1,
totalamount: 500
},
{
_id: objectid('668cf766749a72317b175648'),
order_id: 1003,
product: 'keyboard',
quantity: 3,
totalamount: 150
}
]至此,我们学习了如何安装、启动和停止 mongodb,并通过 mongodb shell 执行基础的 crud 操作(创建、查询、更新和删除文档),同时探索了 mongodb 的聚合操作,用于实现复杂的数据分析。后续,会继续研究关于oracle 23ai在json这方面的能力表现。
到此这篇关于mongodb安装、基础操作和聚合实例详解的文章就介绍到这了,更多相关mongodb安装内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论