MongoDb随笔,PyMongo简单使用
生活随笔
收集整理的這篇文章主要介紹了
MongoDb随笔,PyMongo简单使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
安裝MongoDb
【更新2021-07-06】
- https://www.mongodb.com/try/download/community 下載對應系統(tǒng)的軟件版本(CentOS7.9 mongod 4.4.6)
- rpm -ivh mongodb-org-server-4.4.6-1.el7.x86_64.rpm安裝服務
- systemctl start mongod啟動服務
- rpm -ivh mongodb-org-shell-4.4.6-1.el7.x86_64.rpm安裝客戶端程序mongo
【很久很久以前】
MongoDb下載對應的系統(tǒng)版本的可執(zhí)行文件
本人系統(tǒng)環(huán)境:rhel-server-6.2-x86_64
解壓縮包tar zxvf mongodb-linux-x86_64-rhel62-3.0.2.tgz
可以查看目錄下的README,了解各個可執(zhí)行文件的作用。
簡單啟動命令 mkdir db; mongo --dbpath=./db
mongo --help 可以獲取更多幫助。
也可以指定配置文件來更改啟動參數(shù) 參考https://docs.mongodb.com/manual/reference/configuration-options/#configuration-file
安裝PyMongo
安裝命令:pip install pymongo
更多關于pip的應用可參考Python下pip pydoc 2to3等工具
PyMongo簡單使用
#!/usr/bin/env python # -*- coding: utf-8 -*-import pymongo import datetimedef get_db():# 建立連接client = pymongo.MongoClient(host="127.0.0.1", port=27017)db = client['example']#或者 db = client.examplereturn dbdef get_collection(db):# 選擇集合(mongo中collection和database都是延時創(chuàng)建的)coll = db['informations']print(db.collection_names())return colldef insert_one_doc(db):# 插入一個documentcoll = db['informations']information = {"name": "quyang", "age": "25"}information_id = coll.insert(information)print(information_id)def insert_multi_docs(db):# 批量插入documents,插入一個數(shù)組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):# 有就返回一個,沒有就返回Nonecoll = 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來查找一個doccoll = 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 ObjectIdprint("_id 轉換成ObjectId類型")print(coll.find_one({"_id": ObjectId(str(obj_id))}))def get_many_docs(db):# mongo中提供了過濾查找的方法,可以通過各種條件篩選來獲取數(shù)據(jù)集,還可以對數(shù)據(jù)進行計數(shù),排序等處理coll = db['informations']#ASCENDING = 1 升序;DESCENDING = -1降序;default is ASCENDINGfor item in coll.find().sort("age", pymongo.DESCENDING):print(item)count = coll.count()print("集合中所有數(shù)據(jù) %s個" % int(count))#條件查詢count = coll.find({"name":"quyang"}).count()print("quyang: %s"%count)def clear_all_datas(db):#清空一個集合中的所有數(shù)據(jù)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"}))# 查詢表中所有的數(shù)據(jù)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)資源匯總
- 安裝包下載鏈接 https://www.mongodb.com/download-center#community
- 官方手冊 https://docs.mongodb.com/manual/
參考教程:
- MongoDB教程
總結
以上是生活随笔為你收集整理的MongoDb随笔,PyMongo简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery添加div实现消息聊天框
- 下一篇: 【转载保存】Jsoup解析html常用方