MangoDB系列1-快速入门

574 阅读4分钟

MangoDB快速入门

最近爬虫获取到的数据想存储到MongoDB中,先入门下咯

MongoDB Cloud is a platform for building modern, data-driven applications. Start with the data foundation of MongoDB, delivered as a fully managed cloud database, and get developing quickly with our application services.

什么是MongoDB

MangoDB是一款基于C++开发的开源文档数据库,在数据库中数据是通过key-value的形式存储的,类似Python中的字典一样。

在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。

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

安装

管理包安装

自己的系统是macOS,通过包管理工具Homebrew来安装

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
brew updatebrew install mongodb
# 启动mongod --config /usr/local/etc/mongod.conf # 是mongod

手动安装

安装
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
cd ~/book/program    # 指定安装路径curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.4.tgz    # 下载安装包tar –zxvf mongodb-osx-x86_64-3.4.4.tgz  # 解压包mkdir –p mongodb   # 次级目录cp –R –n mongodb-osx-x86_64-3.4.4/ mongodb  # 复制文件到目录下
  1. 在路径~/book/program/mongodb/bin文件夹下面有各个重要的文件。

手动添加2个配置文件:

  • log
  • data
  1. 在配置文件~book/program/mongodb/bin/mongodb.conf中写入下面的内容:
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
systemLog:      destination: file    path: log/mongo.log    logAppend: true    storage:    dbPath: datanet:    bindIp: 127.0.0.1
启动

在终端中先进入到MongoDB的文件夹中,再进行启动MongoDB:

  • ounter(line
  • ounter(line
cd book/program/mongodb/binmongod --config mongodb.conf

启动之后控制台不会出现任何信息,因为日志Log已经写入到了文件中

windows安装

  1. 从MongoDB的官网下载Windows版本的MongoDB,双击各种next,进行下载安装
  2. 安装路径选择在C:\Program Files\MongoDB\
  3. 进入到C:\Program Files\MongoDB\bin中,将看到的全部文件复制到步骤4中
  4. 在C盘中创建C:\MongoDB文件夹,粘贴步骤3中的文件;同时创建2个文件夹:Data和Log
  5. 使用记事本创建配置文件C:\MongoDB\mongod.conf,内容和上面的相同
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
systemLog:      destination: file    path: log\mongo.log    logAppend: true    storage:    dbPath: datanet:    bindIp: 127.0.0.1
  1. 输入cmd,在命令窗口中启动
  • ounter(line
mongod.exe --config mongod.conf

管理工具

安装

常用的管理软件是RoboMongo,现在变成了Robo 3T,直接在官网上下载安装

连接

数据存储

在MongoDB中,数据是按照库database—集合collection—文档document的层级方式存储的,通过Python进行类比

  • 文档:相当于是字典
  • 集合:相当于是包含了很多字典的列表
  • :相当于是一个大字典;大字典中每个键值对都对应了一个集合

PyMongo

PyMongo是Python对MongoDB操作的接口包,能够实现对MongoDB的增删改查和排序等操作

安装

  • ounter(line
pip install pymongo

连接

  1. 使用pymongo初始化数据库连接
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
# 1-直接连接本地from pymongo import MongoClientclient = MongoClient()  
# 2-连接其他服务器:通过统一资源标识符URI-uniform resource identifier来连接地址# 格式 mongodb://用户名:密码@服务器IP或域名:端口号from pymongo import MongoClientclient = MongoClient('mongodb://peter:123456@192.168.1.12:27019')
# 3-没有设置权限验证,不需要用户名和密码from pymongo import MongoClientclient = MongoClient('mongodb://192.168.1.12:27019')

初始化数据库

通常由两种方式来初始化与集合。

方式1:

Chart6和spider都不是变量名,是库和集合的名字

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
from pymongo import MongoClientclient = MongoClient()database = client.Charter6    # Charter6是库的名字collection = database.spider  # spider是集合的名字

方式2:

方括号中指定库名和集合名字

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
from pymongo import MongoClientclient = MongoClient()database = client["Charter6"]collection = db1['spider']  #  指定库名和集合名
# 方式2中指定变量db_name = "Charter6"col_name = "spider"database = client[db_name]collection = client[col_name]

当存在多个数据库或者集合名字的时候,通过方式2,以列表的形式使用循环来操作

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
database_name_list = ['db1','db2','db3']for db in database_name_list:  database = client[db]  collection = db.test

访问权限

默认情况下,MongoDB只允许本机访问,不允许外网访问。如果需要外网访问,修改配置文件:

  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
systemLog:      destination: file    path: log\mongo.log    logAppend: true    storage:    dbPath: datanet:    #bindIp: 127.0.0.1  # 这里只允许本机    bindIp: 0.0.0.0   # 允许全部,或者指定某个地址