这里是简单的安装和使用记录,首先要有一个可用的mongo环境,Win环境或者Linux环境都可以。 假定你对mongo有所了解和知道一些命令
这里是简单的安装和使用记录,首先要有一个可用的mongo环境,win环境或者linux环境都可以。 假定你对mongo有所了解和知道一些命令行操作。
安装和更新
跟大多数py包安装一样,可以源码安装,也可以使用pip或者easy_install来安装
安装
pip install pymongo
升级
pip install --upgrade pymongo
其他安装方法请参照文档pymongo安装
操作
官网教程
小案例
#-*- coding: utf-8 -*-
#python2.7x
#author: orangleliu @2014-09-24
'''
pymongo的简单使用
'''
from pymongo import MongoClient
def get_db():
#建立连接
client = MongoClient("localhost", 27017)
#test,还有其他写法
db = client.test
return db
def get_collection(db):
#选择集合(mongo中collection和database都是lazy创建的,具体可以google下)
collection = db['posts']
print collection
def insert_one_doc(db):
#插入一个document
posts = db.posts
post = {"name":"lzz", "age":25, "weight":"55"}
post_id = posts.insert(post)
print post_id
def insert_mulit_docs(db):
#批量插入documents,插入一个数组
posts = db.posts
post = [ {"name":"nine", "age":28, "weight":"55"},
{"name":"jack", "age":25, "weight":"55"}]
obj_ids = posts.insert(post)
print obj_ids
##查询,可以对整个集合查询,可以根ObjectId查询,可以根据某个字段查询等
def get_all_colls(db):
#获得一个数据库中的所有集合名称
print db.collection_names()
def get_one_doc(db):
#有就返回一个,没有就返回None
posts = db.posts
print posts.find_one()
print posts.find_one({"name":"jack"})
print posts.find_one({"name":"None"})
return
def get_one_by_id(db):
#通过objectid来查找一个doc
posts = db.posts
obj = posts.find_one()
obj_id = obj["_id"]
print "_id 为ObjectId类型 :"
print posts.find_one({"_id":obj_id})
#需要注意这里的obj_id是一个对象,不是一个str,,使用str类型作为_id的值无法找到记录
print "_id 为str类型 "
print posts.find_one({"_id":str(obj_id)})
#可以通过ObjectId方法把str转成ObjectId类型
from bson.objectid import ObjectId
print "_id 转换成ObjectId类型"
print posts.find_one({"_id":ObjectId(str(obj_id))})
def get_many_docs(db):
#mongo中提供了过滤查找的方法,可以通过各
#种条件筛选来获取数据集,还可以对数据进行计数,排序等处理
posts = db.posts
#所有数据,按年龄排序, -1是倒序
all = posts.find().sort("age", -1)
count = posts.count()
print "集合中所有数据 %s个"%int(count)
for i in all:
print i
#条件查询
count = posts.find({"name":"lzz"}).count()
print "lzz: %s"%count
for i in posts.find({"name":"lzz", "age":{"$lt":20}}):
print i
def clear_coll_datas(db):
#清空一个集合中的所有数据
db.posts.remove({})
if __name__ == "__main__":
db = get_db()
obj_id = insert_one_doc(db)
obj_ids = insert_mulit_docs(db)
#get_all_colls(db)
#get_one_doc(db)
#get_one_by_id(db)
#get_many_docs(db)
clear_coll_datas(db)
这都是写简单的操作,至于集合操作,group操作等以后在总结。
MongoDB的Python驱动PyMongo
安装MongoDB 开发环境PyMongo
本文永久更新链接地址:

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
