MongoDB数据存取

第一步, 假设已经安装好了MongoDB, 使用管理员身份运行cmd,启动MongoDB服务。一般cmd.exe 在C:\Windows\System32中,找到cmd.exe-右击-以管理员身份运行。

MongoDB和charts库的使用

第二步, 在python中运行MongoDB, 实现对数据的存取

import pymongo
# 创建数据库
client = pymongo.MongoClient(host = 'localhost', port = 27017)

# client = pymongo.MongoClient('mongodb://localhost:27017')
# 标识数据
db = client.test

# db = client['test']
# 指定集合, 相当于一个工作表 
collection = db.student
student = {'id':'20170101',
          'name':'Jordan',
          'age':20,
          'gender':'male'}
# 插入单条数据
result = collection.insert_one(student)
print(result)

student1 = {'id':'20170101',
          'name':'Jordan',
          'age':20,
         'gender':'male'}
student2 = {'id':'20170101',
           'name':'Mike',
           'age':20,
           'gender':'male'}
# 插入多条数据
results = collection.insert_many([student1, student2])
print(results)
# 查找 id 号
print(results.inserted_ids)
# 查找名字为 Mike 的结果,返回匹配的第一个结果
result = collection.find_one({'name':'Mike'})
print(type(result))
print(result)
# 查找所有年龄为20 的结果, 返回所有结果,需要进行循环遍历
results = collection.find({'age':20})
print(results)
for result in results:
   print(result)

# 查找所有年龄大于20 的结果, 返回所有结果,需要进行循环遍历

results = collection.find({'age':{'$gt':20}})
print(results)
for result in results:
    print(result)
# 正则匹配
results = collection.find({'name':{'$regex':'^M.*'}})
print(results)
for result in results:
    print(result)
# 计数
count = collection.find({'age':20}).count()
print(count)
# 排序,按字母顺序升序排列,  skip(2) 跳过前两个,limit(2) 指定选取个数为2个
results = collection.find().sort('name',pymongo.ASCENDING).skip(2).limit(2)
print([result['name'] for result in results])
# 设置条件
condition = {'name':'Mike'}
# 找到满足条件的数据
student = collection.find_one(condition) 
# 更改数据
student['age'] = 25
# 更新数据
result = collection.update(condition,student)
print(result)
# 设置条件, 选取年龄大于20的数据
condition = {'age':{'$gt':20}}
# 更新数据, {'$inc':{'age':1}}对满足年龄大于20的数据加1
result = collection.update_one(condition,{'$inc':{'age':1}})
print(result)
# result.matched_count 匹配数目, result.modified_count 更新数目
print(result.matched_count, result.modified_count)
# 删除数据
result = collection.remove({'name':'Jordan'})
print(result)

数据特征的提取与可视化

第一步, 同上面第一步。

第二步,定位到MongoDB安装目录,小编的安装位置是E:\license\MongoDB\bin。

MongoDB和charts库的使用

第三步, 将数据库导入MongoDB

mongoimport --db goods (#数据库名称 )-- collection beijing (# 数据表名称 )-- file F:\PycharmProjects\爬虫入门训练营\作业18\sample.json (# 导入数据) – numInsertionWorkers 100 (# 多进程导入)
MongoDB和charts库的使用

第四步, 连接数据库。

import pymongo
# 连接数据库
client = pymongo.MongoClient('localhost', 27017) # # 指定主机, 指定端口号

# 查找数据库
data = client['goods']

# 查找数据表
item_info = data['beijing']

# 查看前五条数据
for i in item_info.find().limit(5):
    print(i)

MongoDB和charts库的使用

第五步,导入charts库 , 进行可视化。

# 导入可视化工具
import charts
series = [{'name':'OS X','data':[11],'type':'column',},
          {'name':'Ubuntu', 'data':[8], 'type':'column', 'color':'#ff0066'},
          {'name':'Windows','data':[12], 'type':'column'},
          {'name':'Others','data':[29], 'type':'column'}]
series2 = [{'name':'John','data':[5],'type':'column'},{'name':'John', 'data':[5], 'type':'column'}]

#  绘图
charts.plot(series, show = 'inline')

MongoDB和charts库的使用

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-04
  • 2022-01-29
  • 2022-12-23
  • 2021-10-05
  • 2021-06-02
  • 2022-12-23
  • 2021-04-21
相关资源
相似解决方案