PyMongo安裝和使用
這裡是簡單的安裝和使用記錄,首先要有一個可用的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操作等以後在總結。
本文出自 “orangleliu筆記本” 部落格,請務必保留此出處 http://blog.csdn.net/orangleliu/article/details/39545751
相關文章
- 【mongodb】安裝pymongoMongoDB
- Python安裝PyMongo的方法詳細介紹PythonGo
- pymongo的基本使用Go
- MongoDB和pymongo自用手冊MongoDB
- PyMongo 基礎使用指南Go
- Pymongo Tutorial & Pymongo入門教Go
- Elasticsearch 安裝和使用Elasticsearch
- hydra 安裝和使用
- ActiveMq安裝和使用MQ
- httprunner 安裝和使用HTTP
- docker安裝和使用Docker
- nginx 安裝和使用Nginx
- goaccess安裝和使用Go
- Webmin 安裝和使用Web
- Go的安裝和使用Go
- yarn的安裝和使用Yarn
- CMake的安裝和使用
- SSDB安裝和使用初探
- azkaban的安裝和使用
- 安裝和使用 Composer
- Mac 安裝和使用redisMacRedis
- Angular CLI 安裝和使用Angular
- gulp_安裝和使用
- PyCharm 首次安裝和使用PyCharm
- 安裝和使用memcached(windows)Windows
- FTP的安裝和使用FTP
- 如何安裝和使用DockerDocker
- GitLab 的安裝和使用Gitlab
- 使用docker安裝mysql和redisDockerMySqlRedis
- Python的安裝和使用Python
- proxysql安裝和使用小記SQL
- kafka入門安裝和使用Kafka
- webpack的安裝和基本使用Web
- geoserver PostGIS的安裝和使用Server
- VMware Workstation 的安裝和使用
- Docker安裝和簡單使用Docker
- Jetty的安裝、配置和使用Jetty
- CocoaPods安裝和使用(SDWebImage)Web