MongoDB

MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。

在高负载的情况下,添加更多的节点,可以保证服务器性能。

MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。

MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。

MongoDB Atlas

MongoDB Atlas 提供的是云端的数据库服务,用来练手是非常不错的。

如果你想在本地安装 MongoDB 可以去官网下载,MongoDB 支持 Windows、OSX、Linux,虽然你可以在你的电脑上下载安装 MongoDB,但作为初学研究学习,其实没必要这么折腾,除非你天天在本机用。所以,一个更好的使用方法就是云MongoDB,云 MongoDB 就是把 MongoDB 安装在远程的服务器上,并对外暴露一个服务地址,我们用这个服务地址来连接数据库进行操作。

使用云数据库及 Atlas 的好处在于,可以支持更大规模的存储,更安全,是免本地安装并且无需手动开启,每次直接链接即可 ,维护简单,不需要我们去维护数据的升级、安装。

登录注册

先在MongoDB这里注册一个账号,记得挂个梯子

创建数据库

左侧选择database,点击右侧的”create”,创建一个共享的cluster,选择配置如下

接着向数据库中导入Sample,便于我们进行测试

权限设置

选择用户权限,左侧选择”Database access”,把自己加进去

接着设置网络权限,可以设置成”0.0.0.0/0”,这样就算梯子经常变换ip,也可以连上数据库。

连接MongoDB

打开vscode,安装插件”MongoDB For Vs Code”,安装成功之后左侧会出现一个图标

点击”connect”,根据提示,使用如下形式进行连接,记得把用户名和密码换成自己的

1
mongodb+srv://username:<password>@cluster0.w5bshz2.mongodb.net/

连接成功之后,会可以看见之前导入的样本数据

接着新建playground,就可以使用python对数据库进行操作了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* global use, db */
// MongoDB Playground
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
// Make sure you are connected to enable completions and to be able to run a playground.
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
// The result of the last command run in a playground is shown on the results panel.
// By default the first 20 documents will be returned with a cursor.
// Use 'console.log()' to print to the debug output.
// For more documentation on playgrounds please refer to
// https://www.mongodb.com/docs/mongodb-vscode/playgrounds/

// Select the database to use.
use('mongodbVSCodePlaygroundDB');

// Insert a few documents into the sales collection.
db.getCollection('sales').insertMany([
{ 'item': 'abc', 'price': 10, 'quantity': 2, 'date': new Date('2014-03-01T08:00:00Z') },
{ 'item': 'jkl', 'price': 20, 'quantity': 1, 'date': new Date('2014-03-01T09:00:00Z') },
{ 'item': 'xyz', 'price': 5, 'quantity': 10, 'date': new Date('2014-03-15T09:00:00Z') },
{ 'item': 'xyz', 'price': 5, 'quantity': 20, 'date': new Date('2014-04-04T11:21:39.736Z') },
{ 'item': 'abc', 'price': 10, 'quantity': 10, 'date': new Date('2014-04-04T21:23:13.331Z') },
{ 'item': 'def', 'price': 7.5, 'quantity': 5, 'date': new Date('2015-06-04T05:08:13Z') },
{ 'item': 'def', 'price': 7.5, 'quantity': 10, 'date': new Date('2015-09-10T08:43:00Z') },
{ 'item': 'abc', 'price': 10, 'quantity': 5, 'date': new Date('2016-02-06T20:20:13Z') },
]);

// Run a find command to view items sold on April 4th, 2014.
const salesOnApril4th = db.getCollection('sales').find({
date: { $gte: new Date('2014-04-04'), $lt: new Date('2014-04-05') }
}).count();

// Print a message to the output window.
console.log(`${salesOnApril4th} sales occurred in 2014.`);

// Here we run an aggregation and open a cursor to the results.
// Use '.toArray()' to exhaust the cursor to return the whole result set.
// You can use '.hasNext()/.next()' to iterate through the cursor page by page.
db.getCollection('sales').aggregate([
// Find all of the sales that occurred in 2014.
{ $match: { date: { $gte: new Date('2014-01-01'), $lt: new Date('2015-01-01') } } },
// Group the total sales for each product.
{ $group: { _id: '$item', totalSaleAmount: { $sum: { $multiply: [ '$price', '$quantity' ] } } } }
]);