程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python series articles - detailed explanation of Python operation mongodb

編輯:Python
  • mongodb
# Import MongoClient modular
from pymongo import MongoClient, ASCENDING, DESCENDING
# Two ways
#1. Incoming database IP And port number
mc = MongoClient('127.0.0.1', 27017)
#2. Pass in the connection string directly
mc = MongoClient('mongodb://127.0.0.1:27017')
# Connection with password
# First specify the connection testdb database
# db = mc.testdb
# adopt authenticate Method authentication account password
# db.authenticate('username','password')
# Check if the connection is successful , The following results are output to indicate that the connection is successful
print(mc.server_info())
# Specify two ways to manipulate the database
#1. obtain testdb database , If not, create it automatically
db = mc.testdb
#2. Effect and above db = mc.testdb identical
db = mc['testdb']
# Print out testdb All sets under the database ( surface )
print(db.collection_names())
# There are two ways to specify the set of operations
#1. obtain test aggregate , If not, create it automatically
collection = db.test
#2. Effect and collection = db.test identical
collection = db['test']
# Print a row of data in the set
print(collection.find_one())
book = {
'name' : 'Python Basics ',
'author' : ' Zhang San ',
'page' : 80
}
# Insert a record into the collection
collection.insert_one(book)
# about insert_many() Method , We can pass data as a list of parameters
book1 = {
'name' : 'Java Basics ',
'author' : ' Li Bai ',
'page' : 100
}
book2 = {
'name' : 'Java virtual machine ',
'author' : ' Wang Wu ',
'page' : 100
}
# establish book_list list
book_list = [book1, book2]
# Insert multiple records into the collection
collection.insert_many(book_list)
# Query a record by condition , Returns if it does not exist None
res = collection.find_one({'author': ' Zhang San '})
print (res)
# Query multiple records by criteria , Returns if it does not exist None
res = collection.find({'page': 100})
print (res)
# Use find() The query will return an object
# Traversing objects , And print the query results
for r in res:
print(r)
# Inquire about page Greater than 50 The record of
res = collection.find({'page': {'$gt': 50}})
book = collection.find_one({'author': ' Zhang San '})
book['page'] = 90
# The update meets the conditions {'author', ' Zhang San '} The first record
res = collection.update_one({'author': ' Zhang San '}, {'$set': book})
# The result of the update is an object , We can call matched_count and modified_count Property to obtain the number of matched data and the number of affected data .
print(res.matched_count, res.modified_count)
# The update meets the conditions page>90 All records ,page Field self addition 10
res = collection.update_many({'page': {'$gt': 90}}, {'$inc': {'page': 10}})
# Number of records matched and affected by print update
print(res.matched_count, res.modified_count)
book3 = {'name':'Python senior ', 'author':' Zhao Fei ', 'page': 50}
#upsert=True Indicates that if there are no records that meet the update conditions , Will be book3 Insert into the collection
res = collection.update_one({'author': ' Zhao Fei '}, {'$set': book3}, upsert=True)
print(res.matched_count, res.modified_count)
# Check all records , And traverse and print out
res = collection.find()
for r in res:
print(r)
# Delete the first record that meets the conditions
result = collection.delete_one({'author': ' Zhang San '})
# You can also return an object by deleted_count Number of records deleted by attribute query
print(result.deleted_count)
# Delete all records that meet the conditions , Here is the deletion page < 90 The record of
result = collection.delete_many({'page': {'$lt': 90}})
print(result.deleted_count)
# The query returns the records that meet the conditions and deletes them
result = collection.find_one_and_delete({'author': ' Wang Wu '})
print(result)
# Count the number of query results
# Number of all results
collection.find().count()
# The number of results satisfying the conditions
collection.find({'page': 100}).count()
# Query results are sorted by field
# Ascending
results = collection.find().sort('page', ASCENDING)
# Descending
results = collection.find().sort('page', DESCENDING)
# The following query results are based on page Ascending sort , Only the second record and the next two results are returned
results = collection.find().sort('page', ASCENDING).skip(1).limit(2)
print(results)
# unique=True when , Create a unique index , When the index field inserts the same value, an error will be automatically reported , The default is False
collection.create_index('page', unique= True)
# Print out the created index
print(collection.index_information())
# Delete index
collection.drop_index('page_1')
# Delete the collection
collection.drop()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved