摘要
本文介绍了MongoDB常用查询指令及shell用法,让我们拿下它。
初始化数据库
先手动插入6条集合数据,放入mongodb.json
{"_id":"ORDER_001","orderDate":{"$date":"2024-10-23T10:30:00Z"},"orderStatus":"shipped","customer":{"customerId":"CUST_8901","name":"李明","contact":{"email":"liming@example.com","phone":"13800138001","address":{"street":"花园路 123 号","city":"上海","province":"上海","Codepostal":"200000","country":"中国"}},"membership":{"level":"gold","points":1500,"discount":0.1}},"shipping":{"shippingId":"SHIP_0056","carrier":"顺丰速运","trackingNumber":"SF1234567890","shippingAddress":{"street":"科技大道 45 号","city":"北京","province":"北京","postalCode":"100000","country":"中国"},"shippingDate":{"$date":"2024-10-25T14:00:00Z"},"estimatedDelivery":"2024-10-30T16:00:00Z"},"payment":{"paymentId":"PAY_7890","paymentMethod":"creditCard","paymentDate":{"$date":"2024-10-23T01:35:00Z"},"amount":2499.00,"currency":"CNY","paymentStatus":"completed"},"items":[{"itemId":"ITEM_001","product":{"productId":"PROD_4567","name":"智能手机","category":"electronics","description":"高性能智能手机,6.7 英寸屏幕,5G 网络,128GB 存储","price":399.900,"specs":{"processor":"骁龙 8 系列","ram":"8GB","battery":"4500mAh","color":"黑色"}},"quantity":1,"subtotal":3999.00,"discount":0.25,"total":2999.25},{"itemId":"ITEM_002","product":{"productId":"DPRO_8910","name":"无线耳机","category":"electronics","description":"高音质无线蓝牙耳机,主动降噪,舒适佩戴","price":899.00,"specs":{"batteryLife":"20 小时","bluetoothVersion":"5.2","color":"白色"}},"quantity":1,"subtotal":899.00,"discount":0.1,"total":809.10}],"total":{"subTotal":4898.00,"discounts":989.65,"tax":194.90,"shippingFee":0.00,"grandTotal":4103.25}}
{"_id":"ORDER_002","orderDate":{"$date":"2024-10-24T14:20:00Z"},"orderStatus":"processing","customer":{"customerId":"CUST_8902","name":"张伟","contact":{"email":"zhangwei@example.com","phone":"13800138002","address":{"street":"和平路 45 号","city":"天津","province":"天津","postalCode":"300000","country":"中国"}},"membership":{"level":"silver","points":800,"discount":0.05}},"shipping":{"shippingId":"SHIP_0057","carrier":"圆通速递","trackingNumber":"YTO987654321","shippingAddress":{"street":"民主路 67 号","city":"天津","province":"天津","postalCode":"300000","country":"中国"},"shippingDate":{"$date":"2024-10-26T09:00:00Z"},"estimatedDelivery":"2024-11-01T18:00:00Z"},"payment":{"paymentId":"PAY_7891","paymentMethod":"alipay","paymentDate":{"$date":"2024-10-24T14:25:00Z"},"amount":1299.00,"currency":"CNY","paymentStatus":"completed"},"items":[{"itemId":"ITEM_003","product":{"productId":"PROD_4568","name":"平板电脑","category":"electronics","description":"10.2 英寸平板电脑,A13 芯片,64GB 存储","price":2599.00,"specs":{"processor":"A13","ram":"4GB","battery":"6500mAh","color":"银色"}},"quantity":1,"subtotal":2599.00,"discount":0.15,"total":2209.15},{"itemId":"ITEM_004","product":{"productId":"PROD_8911","name":"智能手表","category":"electronics","description":"多功能智能手表,心率监测,运动追踪","price":799.00,"specs":{"batteryLife":"7 天","bluetoothVersion":"5.0","color":"黑色"}},"quantity":1,"subtotal":799.00,"discount":0.1,"total":719.10}],"total":{"subTotal":3398.00,"discounts":539.75,"tax":169.90,"shippingFee":0.00,"grandTotal":2968.15}}
{"_id":"ORDER_003","orderDate":{"$date":"2024-10-24T14:20:00Z"},"orderStatus":"shipped","customer":{"customerId":"CUST_8903","name":"王芳","contact":{"email":"wangfang@example.com","phone":"13800138003","address":{"street":"建设路 78 号","city":"重庆","province":"重庆","postalCode":"400000","country":"中国"}},"membership":{"level":"platinum","points":3200,"discount":0.15}},"shipping":{"shippingId":"SHIP_0058","carrier":"京东物流","trackingNumber":"JD9876543210","shippingAddress":{"street":"解放路 34 号","city":"重庆","province":"重庆","postalCode":"400000","country":"中国"},"shippingDate":{"$date":"2024-10-24T14:20:00Z"},"estimatedDelivery":"2024-10-31T14:00:00Z"},"payment":{"paymentId":"PAY_7892","paymentMethod":"wechatpay","paymentDate":{"$date":"2024-10-24T14:20:00Z"},"amount":3899.00,"currency":"CNY","paymentStatus":"completed"},"items":[{"itemId":"ITEM_005","product":{"productId":"PROD_4569","name":"笔记本电脑","category":"electronics","description":"15.6 英寸笔记本电脑,i7 处理器,16GB 内存,512GB SSD","price":5999.00,"specs":{"processor":"i7","ram":"16GB","battery":"5600mAh","color":"深空灰"}},"quantity":1,"subtotal":5999.00,"discount":0.2,"total":4799.20},{"itemId":"ITEM_006","product":{"productId":"PROD_8912","name":"机械键盘","category":"electronics","description":"机械键盘,青轴,背光,游戏专用","price":499.00,"specs":{"switchType":"青轴","backlight":"RGB","layout":"全键"}},"quantity":1,"subtotal":499.00,"discount":0.1,"total":449.10}],"total":{"subTotal":6498.00,"discounts":1299.70,"tax":324.90,"shippingFee":0.00,"grandTotal":5522.20}}
{"_id":"ORDER_004","orderDate":{"$date":"2023-10-24T14:20:00Z"},"orderStatus":"pending","customer":{"customerId":"CUST_8904","name":"刘强","contact":{"email":"liuqiang@example.com","phone":"13800138004","address":{"street":"中山路 90 号","city":"广州","province":"广东","postalCode":"510000","country":"中国"}},"membership":{"level":"bronze","points":200,"discount":0.02}},"shipping":{"shippingId":"SHIP_0059","carrier":"中通快递","trackingNumber":"ZTO123456789","shippingAddress":{"street":"天河路 56 号","city":"广州","province":"广东","postalCode":"510000","country":"中国"},"shippingDate":{"$date":"2023-10-24T14:20:00Z"},"estimatedDelivery":"2024-11-03T17:00:00Z"},"payment":{"paymentId":"PAY_7893","paymentMethod":"bankTransfer","paymentDate":{"$date":"2023-10-24T14:20:00Z"},"amount":1599.00,"currency":"CNY","paymentStatus":"pending"},"items":[{"itemId":"ITEM_007","product":{"productId":"PROD_4570","name":"数码相机","category":"electronics","description":"2420 万像素数码相机,4K 视频,WiFi 连接","price":3999.00,"specs":{"sensor":"APS-C","megapixels":24.2,"zoom":"18-55mm","color":"黑色"}},"quantity":1,"subtotal":3999.00,"discount":0.1,"total":3599.10},{"itemId":"ITEM_008","product":{"productId":"PROD_8913","name":"蓝牙音箱","category":"electronics","description":"便携式蓝牙音箱,立体声,防水设计","price":299.00,"specs":{"batteryLife":"12 小时","bluetoothVersion":"5.0","waterproof":"IPX7"}},"quantity":1,"subtotal":299.00,"discount":0.05,"total":284.05}],"total":{"subTotal":4298.00,"discounts":429.80,"tax":214.90,"shippingFee":0.00,"grandTotal":4082.10}}
{"_id":"ORDER_005","orderDate":{"$date":"2023-10-24T14:20:00Z"},"orderStatus":"shipped","customer":{"customerId":"CUST_8905","name":"赵敏","contact":{"email":"zhaomin@example.com","phone":"13800138005","address":{"street":"南京路 110 号","city":"南京","province":"江苏","postalCode":"210000","country":"中国"}},"membership":{"level":"gold","points":2100,"discount":0.1}},"shipping":{"shippingId":"SHIP_0060","carrier":"韵达快递","trackingNumber":"YD9876543210","shippingAddress":{"street":"汉中路 78 号","city":"南京","province":"江苏","postalCode":"210000","country":"中国"},"shippingDate":{"$date":"2023-10-24T14:20:00Z"},"estimatedDelivery":"2024-11-04T15:00:00Z"},"payment":{"paymentId":"PAY_7894","paymentMethod":"creditCard","paymentDate":{"$date":"2023-10-24T14:20:00Z"},"amount":2399.00,"currency":"CNY","paymentStatus":"completed"},"items":[{"itemId":"ITEM_009","product":{"productId":"PROD_4571","name":"游戏主机","category":"electronics","description":"高性能游戏主机,4K HDR,1TB 存储","price":2999.00,"specs":{"processor":"定制 CPU","gpu":"定制 GPU","storage":"1TB SSD","color":"黑色"}},"quantity":1,"subtotal":2999.00,"discount":0.2,"total":2399.20}],"total":{"subTotal":2999.00,"discounts":599.80,"tax":149.95,"shippingFee":0.00,"grandTotal":2549.15}}
{"_id":"ORDER_006","orderDate":{"$date":"2023-10-24T14:20:00Z"},"orderStatus":"processing","customer":{"customerId":"CUST_8906","name":"孙雨","contact":{"email":"sunny@example.com","phone":"13800138006","address":{"street":"成都路 124 号","city":"成都","province":"四川","postalCode":"610000","country":"中国"}},"membership":{"level":"silver","points":650,"discount":0.07}},"shipping":{"shippingId":"SHIP_0061","carrier":"德邦快递","trackingNumber":"DB9876543210","shippingAddress":{"street":"锦江路 56 号","city":"成都","province":"四川","postalCode":"610000","country":"中国"},"shippingDate":{"$date":"2023-10-24T14:20:00Z"},"estimatedDelivery":"2024-11-06T16:00:00Z"},"payment":{"paymentId":"PAY_7895","paymentMethod":"alipay","paymentDate":{"$date":"2023-10-24T14:20:00Z"},"amount":1799.00,"currency":"CNY","paymentStatus":"completed"},"items":[{"itemId":"ITEM_010","product":{"productId":"PROD_4572","name":"智能音箱","category":"electronics","description":"智能语音助手,高品质音频,多房间音乐","price":499.00,"specs":{"audioOutput":"360 度环绕","connectivity":"WiFi, Bluetooth","color":"白色"}},"quantity":2,"subtotal":998.00,"discount":0.1,"total":898.20},{"itemId":"ITEM_011","product":{"productId":"PROD_8914","name":"电动牙刷","category":"health","description":"声波电动牙刷,智能计时,多种清洁模式","price":399.00,"specs":{"batteryLife":"30 天","chargingType":"感应充电","color":"蓝色"}},"quantity":1,"subtotal":399.00,"discount":0.05,"total":379.05},{"itemId":"ITEM_012","product":{"productId":"PROD_8915","name":"电子血压计","category":"health","description":"上臂式电子血压计,高精度测量,大屏幕显示","price":299.00,"specs":{"measurementType":"上臂式","display":"LCD","memory":"60 组数据"}},"quantity":1,"subtotal":299.00,"discount":0.03,"total":289.13}],"total":{"subTotal":1696.00,"discounts":179.60,"tax":84.80,"shippingFee":0.00,"grandTotal":1601.20}}
或单个json插入
MongoDB Compass界面直接查询
第一种方式,直接{}查询
指令
#匹配name字段
{orderStatus: "shipped"}
#查找 customer.name 子字段为“李明”的文档
{"customer.name": "李明"}
#使用 $regex 操作符查找 paymentMethod 值包含“Card”术语的所有文档
{ "payment.paymentMethod": { $regex: 'Card' } }
#按多个条件匹配 ($and)
{$and:[{"orderStatus":"shipped","total.shippingFee":0}]}
#regex组合使用
{and:[{"orderStatus":"shipped","total.shippingFee":0,"shipping.carrier":{regex:'顺丰'}}]}
#按多个可能条件匹配 (or 的每个条件都需要是一个独立的对象
{$or:[{"_id":"ORDER_001"},{"_id":"ORDER_002"}]}
#按排除匹配($not)
{"_id":{not:{eq:"ORDER_001"}}}
#使用比较操作符进行匹配 查找total.subTotal在4000-5000的所有文档
{and:[{"total.subTotal":{gte:4000}},{"total.subTotal":{$lte:5000}}]}
#使用$gt操作符和Date()方法查找orderDate字段值晚于2023-10-26的所有文档
注意这里的时间字段需要使用"orderDate":{"$date":"2024-10-23T10:30:00Z"}创建
{orderDate:{$gt: new Date('2023-10-26')}}
MongoDB的Shell查询窗口第二种方式,右上角shell查询
指令:
将以上所有指令包在find()内回车即可
use 数据库
db["集合"],find(指令)
总结
以上我们了解了MongoDB常用查询指令及shell用法,个人觉得这些指令操作起来还是有点麻烦,不过MongDB Compass本身也提供了AI键入和多个界面显示。最后官方在代码层还是提供了一些简便的api操作,待集成进SpringBoot使用后展示。
关注公众号:咖啡Beans
在这里,我们专注于软件技术的交流与成长,分享开发心得与笔记,涵盖编程、AI、资讯、面试等多个领域。无论是前沿科技的探索,还是实用技巧的总结,我们都致力于为大家呈现有价值的内容。期待与你共同进步,开启技术之旅。