当前位置:网站首页>Read and write mongodb database files
Read and write mongodb database files
2022-07-25 09:27:00 【Ride Hago to travel】
First , stay ubuntu16.04 Upper use Docker Start a MongoDB 4.0.13 The container of , The basic operation is as follows :
[email protected]:~$ sudo -i
[sudo] hpl Password :
[email protected]:~# systemctl start docker
[email protected]:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[email protected]:~# docker pull mongo:4.0.13
4.0.13: Pulling from library/mongo
976a760c94fc: Pull complete
c58992f3c37b: Pull complete
0ca0e5e7f12e: Pull complete
f2a274cc00ca: Pull complete
7f6568107a70: Pull complete
08957b2477b2: Pull complete
a66dbd57d2a3: Pull complete
135a9132a862: Pull complete
c9b7c17ba1df: Pull complete
7b595c9e65de: Pull complete
5e74b440cab6: Pull complete
9c04ed8f6ca0: Pull complete
3dbe824121e4: Pull complete
Digest: sha256:1aea2377c5b17dcd5e67d4c33f8f7ee09da62a4dd27ae988958b70efdf3a0a6a
Status: Downloaded newer image for mongo:4.0.13
docker.io/library/mongo:4.0.13
[email protected]:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo 4.0.13 0712bd00d695 2 years ago 416MB
[email protected]:~# docker run -d -p 27017:27017 --name mongo4.0 0712bd00d695
1e81e661666a5aa01df516663058de8b768a7b09fafa5c53b832c14a6122a97a
[email protected]:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e81e661666a 0712bd00d695 "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongo4.0
see linux Of IP The address is :192.168.23.129

On the host, you can use Navicat To test the connection :

about 16M Small files within , have access to MongoDB Direct storage :
import os
import datetime
from pymongo import MongoClient
class DBDataSave(object):
client = MongoClient('mongodb://192.168.23.129:27017')
db = client.file_data # Database used
collection = db.datas # Use the collection name in the database
def upload_little_file(self):
upload_dir = r'G:/project/study1/others/mongo_test/upload_files'
file_list = os.listdir(upload_dir)
for file in file_list:
file_path = upload_dir + '/' + file
file_division = file.split('.')
file_size = os.stat(file_path).st_size
f = open(file_path, 'rb')
data = f.read()
doc = [{
"file_name": file,
"filet_type": file_division[-1],
'content': data,
"size": file_size,
'add_time': datetime.datetime.now()
}]
f.close()
self.collection.insert_many(doc)
def download_little_file(self):
if not os.path.exists('./download_files'):
os.mkdir('./download_files')
files = self.collection.find()
for file in files:
with open('./download_files/' + f"{file.get('file_name')}", 'wb') as f:
f.write(file.get('content'))
if __name__ == '__main__':
dds = DBDataSave()
# dds.upload_little_file() # Storage file
dds.download_little_file() # export file 
MongoDB There is a built-in file system named GridFS, Can be used to store more than 16M Above documents :
GridFS Have the ability to manage files in a distributed way , It can break through the general file system pair file The limitation of , Segmented storage , Unlike ordinary file systems, the whole storage . In this way, reading large files will not occupy a lot of memory .
from pymongo import MongoClient
from gridfs import *
import os
class DBDataSaveGF(object):
client = MongoClient('mongodb://192.168.23.129:27017') # Connect local MongoDB database
db = client.file_data
file_put = GridFS(db)
def db_write_file(self):
upload_path = r'G:/project/study1/others/mongo_test/upload_files'
files = os.listdir(upload_path) # List all the files under the folder
for file in files:
file_path = upload_path + '\\' + file
with open(file_path, 'rb') as f1:
self.file_put.put(f1, content_type=file.split('.')[-1], filename=file)
def db_read_file(self):
if not os.path.exists('./download_files'):
os.mkdir('./download_files')
files = self.file_put.find()
# files = file_put.find().sort("uploadDate", -1).limit(1) # Return to the recently uploaded 1 File
for file in files:
with open('./download_files/' + file.filename, 'wb') as f1:
f1.write(file.read())
if __name__ == '__main__':
db = DBDataSaveGF()
# db.db_write_file() # Storage file
db.db_read_file() # Read the file 边栏推荐
- matplotlib数据可视化三分钟入门,半小时入魔?
- MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
- office文件对应的Content-Type类型
- 什么是单机、集群与分布式?
- redis操作利用游标代替keys
- Notes on in-depth analysis of C language 1
- 无法再web服务器上启动调试,web服务器未能找到请求资源
- Notes on in-depth analysis of C language 2
- [GYCTF2020]Node Game
- Unable to start debugging on the web server, the web server failed to find the requested resource
猜你喜欢
![[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line](/img/67/b8c397d78a675014b5e08ceefc88dc.png)
[SCADA case] myscada helps VIB company realize the modernization and upgrading of production line

卷积神经网络的兴趣简单介绍

将list集合的某一字段拼接单个String

Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL

redis的五种数据结构原理分析

jsPDF生成PDF文件,文件不全问题,后台进行文件下载,前台不下载

无法再web服务器上启动调试,web服务器未能找到请求资源

变量名可以用中文?直接把人干蒙了

SSM框架整合,简单案例

使用nexus3发布yum私服(离线-内网)
随机推荐
基本的网络知识
Network principle (2) -- network development
使用nexus3发布yum私服(离线-内网)
Write two channel (stereo) immediately Wav file
registration status: 204
『怎么用』代理模式
『怎么用』装饰者模式
保姆级Scanner类使用详解
初始Flask以及简单地上手应用
Thick willow dustpan, thin willow bucket, who hates reptile man? Asynchronous synergism, half a second to strip away a novel
BigDecimal 对数据进行四舍五入
Deep understanding of static keyword
In mysql, update and select are used together
无法再web服务器上启动调试,web服务器未能找到请求资源
MySQL的索引、视图与事务
抽象类和接口的区别(最详细)
数据库操作语言(DML)
activemq--可持久化机制之KahaDB
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
ActiveMQ -- JDBC with persistent mechanism
