zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python的数据库mongoDB的入门操作

2023-09-11 14:21:34 时间

 

 

Python代码:

 1 import pymongo
 2 
 3 # 获取本地端口,激活mongo客户端
 4 client = pymongo.MongoClient('localhost',27017)
 5 
 6 # 创建一个数据库
 7 mydata = client['mydata']
 8 
 9 # 创建一个表单
10 sheet_tab_one = mydata['sheet_tab_one']
11 
12 # 处理一个本地的txt文档,然后把文本内容全部读取,然后文本数据结构化,并存储每行的文字数,也存出起来
13 # /Users/HeYang/Desktop/含有文本内容的文本文件.txt
14 
15 # path = '/Users/HeYang/Desktop/长江电力分析报告.txt'
16 # with open(path,'r') as f:
17 #     lines = f.readlines()
18 #     for index,line in enumerate(lines):
19 #         if len(line.split())>0 :
20 #             data = {
21 #                 'index':index,
22 #                 'line':line,
23 #                 'words':len(line.split())
24 #             }
25 #             print(data)
26 #             sheet_tab_one.insert_one(data)
27 
28 # 表插入数据的方法insert_one,会不清除原有的数据,重复添加进去
29 
30 # 展示数据库中的数据
31 # $lt $lte $gt $gte $ne,
32 # 依次等价于< <= > >= !=
33 # l表示less,g表示greater e表示equal n表示not
34 for item in sheet_tab_one.find({'index':{'$lt':5}}):
35     print(item)

下面是关于数据库的操作符,需要熟悉并记下来常用的: