使用easy_install pymongo,关于easy_install的安装网上有方法,具体操作是将ez_setup.py下载到本地(路径是https://bootstrap.pypa.io/ez_setup.py),本地执行ez_setup.py文件,然后在环境变量path下加入可执行easy_install的脚本路径...
Python安装pymongo,easy_install pymongo 我安装的版本pymongo是3.0的,所以有些语法和网上找到的不太一样,需要自己调试
<pre name="code" class="python"># coding=utf-8
import pymongo
import datetime
def get_db():
# 建立连接
client = pymongo.MongoClient("10.244.25.180", 27017)
db = client['mydatabase']
print dir(db)
# for coll in db.collection_names:
# print coll
# print type(db._list_collections)
return db
def get_collection(db):
# 选择集合(mongo中collection和database都是延时创建的)
coll = db['informations']
print db.collection_names()
return coll
def insert_one_doc(db):
# 插入一个document
coll = db['informations']
information = {"name": "quyang", "age": "25"}
information_id = coll.insert(information)
print information_id
def insert_multi_docs(db):
# 批量插入documents,插入一个数组
coll = db['informations']
information = [{"name": "xiaoming", "age": "25"}, {"name": "xiaoqiang", "age": "24"}]
information_id = coll.insert(information)
print information_id
def get_one_doc(db):
# 有就返回一个,没有就返回None
coll = db['informations']
print coll.find_one() # 返回第一条记录
print coll.find_one({"name": "quyang"})
print coll.find_one({"name": "none"})
def get_one_by_id(db):
# 通过objectid来查找一个doc
coll = db['informations']
obj = coll.find_one()
obj_id = obj["_id"]
print "_id 为ObjectId类型,obj_id:" + str(obj_id)
print coll.find_one({"_id": obj_id})
# 需要注意这里的obj_id是一个对象,不是一个str,使用str类型作为_id的值无法找到记录
print "_id 为str类型 "
print coll.find_one({"_id": str(obj_id)})
# 可以通过ObjectId方法把str转成ObjectId类型
from bson.objectid import ObjectId
print "_id 转换成ObjectId类型"
print coll.find_one({"_id": ObjectId(str(obj_id))})
def get_many_docs(db):
# mongo中提供了过滤查找的方法,可以通过各种条件筛选来获取数据集,还可以对数据进行计数,排序等处理
coll = db['informations']
# 所有数据,按年龄排序, -1是倒序
all = coll.find().sort("age", -1)
count = coll.count()
print "集合中所有数据 %s个" % int(count)
for i in all:
print i
#条件查询
count = coll.find({"name":"quyang"}).count()
print "quyang: %s"%count
def clear_all_datas(db):
#清空一个集合中的所有数据
print "clear all datas:"
db["informations"].remove({})
if __name__ == '__main__':
db = get_db()
my_collection = get_collection(db)
post = {"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()}
# 插入记录
my_collection.insert(post)
insert_one_doc(db)
# 条件查询
print my_collection.find_one({"x": "10"})
# 查询表中所有的数据
for iii in my_collection.find():
print iii
print my_collection.count()
my_collection.update({"author": "Mike"},
{"author": "quyang", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()})
for jjj in my_collection.find():
print jjj
get_one_doc(db)
get_one_by_id(db)
get_many_docs(db)
# clear_all_datas(db)
动手实践的源码,方便以后回顾......