连接MongoDb数据库 -- Python

1、安装完mongoDb数据库后,如果需要我们的Python程序和MongoDb数据库进行交互,需要安装pymongo模块;

安装方式:采用pip install pymongo的方式

Microsoft Windows [版本 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\licl11092>pip install pymongo
Collecting pymongo
  Downloading pymongo-3.5.1-cp35-cp35m-win_amd64.whl (276kB)
    100% |████████████████████████████████| 276kB 98kB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.5.1

C:\Users\licl11092>

2、Pycharm如何设置MongoDb数据库的可视化

步骤一:

 

简单链接MongoDb:

import pymongo

client = pymongo.MongoClient('localhost',27017)
#1、给创建的数据库命名=给Excel命名
guest = client['guest']
#2、在文件下创建表单=在Excel中添加sheet
sheet_tabs = guest['sheet_tabs']

#3、文件存放路径
path = 'C:/Users/licl11092/Desktop/test/test.txt'
with open(path,'r') as f:
    #一次性读取每一行,并就每行的数据返回为一个列表
    lines = f.readlines()
    for index,value in enumerate(lines,1):
        data = {
            'index':index,
            'value':value,
            'counts':len(value.split())
        }
        #4、往数据库写入数据=在Excel中填写每一行数据
        sheet_tabs.insert_one(data)

#5、将数据库中存放的数据打印输出,find函数中特殊字符的使用方法
for item in sheet_tabs.find({'counts':0}):
    print(item)

 

posted @ 2017-08-30 15:10  我是旺旺  阅读(242)  评论(0编辑  收藏  举报