MongoDB and Python interactive

Drive module

  • pymongo is a common operation in the python drive module MongoDB
  • Available to download and install pip
 pip install pymongo

Create a connection

  • MongoClient is MongoDB client proxy object can be used to perform CRUD operations, but also built connection pool
 from pymongo import MongoClient
 client = MongoClient(host="localhost",port=27017)
 client.admin.authenticate("admin","123456")

Data is written

  • insert_one insert_many two functions and data can be written to MongoDB
 client.school.student.insert_one({"name":"alex"})
 client.school.student.insert_many({"name":"bob"},{"name":"cindy"})

data query

  • find_one and find two functions can be found in the data from MmongDB
 student = client.school.student.find_one({"name":"alex"})
 print(student)
 students = client.school.student.find({})
 for one in students:
    print(one["_id"],one["name"])
  • skip: query for data classification
  • limit: for data paging query
 students = client.school.student.find({}).skip(0).limit(10)
  • count_documents: query total number of records
 count = client.school.student.count_documents({})
  • distinct: the query does not duplicate field
 students = client.school.student.distinct("name")
  • sort: sorting query results
 students = client.school.student.find().sort([("name",-1)])

Data Modification

  • update_one update_many two functions and can modify the data MongoDB
 client.school.student.update_one({"name":"alex"},{"$set":{"sex":"女"}})
 client.school.student.update_many({},{"$set":{"grade":"七年级"}})

Data deletion

  • delete_one and delete_many two functions can be deleted MongoDB data
 client.school.student.delete_one({"name":"alex"})
 client.school.student.delete_many({})

File storage

Connection GridFS

  • GridFS MongoDB is a document storage solutions, primarily for storing more than 16M documents
from gridfs import GridFS
db = client.school
gfs = GridFS(db,collection="book")

save document

  • put function can save the file to the GridFS
file = open("D:/Python编程:从入门到实践.pdf","rb")
args = {"type":"PDF","keyword":"Python"}
gfs.put(file,file="Python编程:从入门到实践.PDF",**args)
file.close()

Find Files

  • find_one and find function can find files stored GridFS
book = gfs.find_one({"filename":"Python编程:从入门到实践.PDF"})
print(book.keywode)
books = gfs.find({"type":"PDF"})
for one in books:
   print(one.filename)

To determine whether the file is stored

  • exists can determine whether a file is stored GridFS
rs = gfs.exists({"filename":"Python编程:从入门到实践.PDF"})
print(rs)

Read the file

  • The get function can read files from GfridFS, and can only be read by the primary key
from bson.objectid import ObjectId
book = gfs.find_one({"filename":"Python编程:从入门到实践.PDF"})
id = book._id
document = gfs.get(ObjectId(id))
file = open("D:/Python从入门到实践.PDF","wb")
file.write(document.read())
file.close()

Delete Files

  • delect function can delete files from GridFS, the same can only remove (the primary key to find the file) by the primary key
from bson.objectid import ObjectId
book = gfs.find_one({"filename":"Python编程:从入门到实践.PDF"})
id = book._id
gfs.delete(ObjectId(id))

Guess you like

Origin www.cnblogs.com/felixqiang/p/11221507.html