欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python?操作?mongodb?數(shù)據(jù)庫詳情

 更新時(shí)間:2022年04月19日 17:34:03   作者:autofelix  
這篇文章主要介紹了python?操作?mongodb?數(shù)據(jù)庫詳情,通過鏈接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫展開內(nèi)容詳細(xì),具有一定的參考價(jià)值,需要的的小伙伴可以參考一下

一、安裝

pip install pymongo

二、連接數(shù)據(jù)庫

import pymongo

# 方式一
client = pymongo.MongoClient('mongodb://localhost:27017')
# 方式二
client = pymongo.MongoClient('localhost',27017)
# 方式三,有密碼認(rèn)證
client = pymongo.MongoClient('localhost', 27017, username='xxx', password='xxx')

三、創(chuàng)建數(shù)據(jù)庫

import pymongo

# 連接
client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test # 或者 db = client['test']
print(db)

四、所有數(shù)據(jù)庫

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
dbs = client.list_database_names()

五、創(chuàng)建集合

  • 也就是數(shù)據(jù)庫中的表
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user # 或者 collections = db['user']
# 刪除表
collections.drop()

六、插入數(shù)據(jù)

  • insert_one:插入一條數(shù)據(jù)
  • insert_many:插入多條數(shù)據(jù)
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 創(chuàng)建文檔數(shù)據(jù)
user1 = {
'name': 'autofelix',
'age': '25',
'height': '172',
'weight': '60'
}

user2 = {
'name': '飛兔小哥',
'age': '28',
'height': '182',
'weight': '70'
}

# 插入一條文檔集合
result = collections.insert_one(user1)
print(result)
print(result.inserted_id)

# 插入多條文檔集合
result = collections.insert_many([user1, user2])
print(result)
print(result.inserted_ids)

七、查詢數(shù)據(jù)

  • find:查詢多條數(shù)據(jù)
  • find_one:查詢一條數(shù)據(jù)
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 查詢所有
collections.find()
# 查詢最近一條
collections.find_one()
# 根據(jù)條件查詢
collections.find_one({'age':25})

八、高級(jí)查詢

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 跳過第一條查到的數(shù)據(jù)
collections.find({'age':{'$gt':10}},['height','age']).skip(1)
# limit限制查詢條數(shù)
collections.find({'age':{'$gt':10}},['height','age']).limit(1)
# 多條件查詢
collections.find_one({'height':{'$gt':150},'age':{'$lt':26,'$gt':10}})
# in查詢,查詢年齡在25,26,32的數(shù)據(jù)
collections.find({'age':{'$in':[25, 26, 32]}})
# or查詢,查詢年齡小于等于23或者大于等于29的數(shù)據(jù)
collections.find({'$or':[{'age':{'$lte':23}}, {'age':{'$gte':29}}]})
# exists查詢
collections.find({'age':{'$exists':True}})
# 正則查詢
collections.find({'name':{'$regex':r'.*auto.*'}})

九、count統(tǒng)計(jì)

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 統(tǒng)計(jì)集合中總共有多少條數(shù)據(jù)
collections.find().count()
# 統(tǒng)計(jì)集合中年齡大于10歲的共有多少條數(shù)據(jù)
collections.find({'age':{'$gt':10}}).count()

十、修改數(shù)據(jù)

  • update_one:修改一條數(shù)據(jù)
  • update_many:修改多條數(shù)據(jù)
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 修改一條數(shù)據(jù)
collections.update_one({'name': 'autofelix'}, {'$set': {'name': '大神'}})
# 修改多條數(shù)據(jù)
collections.update_many({'name': 'autofelix'}, {'$set': {'name': '大神'}})

十一、刪除數(shù)據(jù)

  • delete_one:刪除一條數(shù)據(jù)
  • delete_many:刪除多條數(shù)據(jù)
import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 刪除一條數(shù)據(jù)
collections.delete_one({'name': 'autofelix'})
# 刪除多條數(shù)據(jù)
collections.delete_many({'name': 'autofelix'})
# 刪除所有數(shù)據(jù)
collections.delete_many({})

十二、數(shù)據(jù)排序

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017')
# 創(chuàng)建test數(shù)據(jù)庫
db = client.test
# 創(chuàng)建表
collections = db.user

# 對(duì)字段 age 按升序排序
collections.find().sort('age')
# 對(duì)字段 age 按降序排序
collections.find().sort('age', -1)
# 多字段排序
collections.find().sort((('age',pymongo.ASCENDING),('height',pymongo.ASCENDING)))

到此這篇關(guān)于python 包操作 mongodb 數(shù)據(jù)庫詳情的文章就介紹到這了,更多相關(guān)python 操作mongodb內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決django migrate報(bào)錯(cuò)ORA-02000: missing ALWAYS keyword

    解決django migrate報(bào)錯(cuò)ORA-02000: missing ALWAYS keyword

    這篇文章主要介紹了解決django migrate報(bào)錯(cuò)ORA-02000: missing ALWAYS keyword,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python字符串刪除指定字符的三個(gè)方法

    Python字符串刪除指定字符的三個(gè)方法

    這篇文章主要給大家介紹了關(guān)于Python字符串刪除指定字符的三個(gè)方法,我們?cè)谑褂?nbsp;Python處理字符串的時(shí)候,經(jīng)常會(huì)遇到一些字符串中出現(xiàn)了指定字符,需要的朋友可以參考下
    2023-07-07
  • Python和GO語言實(shí)現(xiàn)的消息摘要算法示例

    Python和GO語言實(shí)現(xiàn)的消息摘要算法示例

    這篇文章主要介紹了Python和GO語言實(shí)現(xiàn)的消息摘要算法示例,本文講解了python消息摘要示例、go語言消息摘要示例及各自的運(yùn)行效果,需要的朋友可以參考下
    2015-03-03
  • Java中的各種單例模式優(yōu)缺點(diǎn)解析

    Java中的各種單例模式優(yōu)缺點(diǎn)解析

    這篇文章主要介紹了Java中的各種單例模式解析,單例模式是Java中最簡(jiǎn)單的設(shè)計(jì)模式之一,這種類型的設(shè)計(jì)模式屬于創(chuàng)建者模式,它提供了一種訪問對(duì)象的最佳方式,需要的朋友可以參考下
    2023-07-07
  • Python中高效的json對(duì)比庫deepdiff詳解

    Python中高效的json對(duì)比庫deepdiff詳解

    deepdiff模塊常用來校驗(yàn)兩個(gè)對(duì)象是否一致,包含3個(gè)常用類,DeepDiff,DeepSearch和DeepHash,其中DeepDiff最常用,可以對(duì)字典,可迭代對(duì)象,字符串等進(jìn)行對(duì)比,使用遞歸地查找所有差異,今天我們就學(xué)習(xí)一下快速實(shí)現(xiàn)代碼和文件對(duì)比的庫–deepdiff
    2022-07-07
  • Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題

    Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題

    這篇文章主要介紹了Python如何根據(jù)關(guān)鍵字逐行提取文本內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 在Python中處理XML的教程

    在Python中處理XML的教程

    這篇文章主要介紹了在Python中處理XML的教程,是Python網(wǎng)絡(luò)編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-04-04
  • python處理文本文件實(shí)現(xiàn)生成指定格式文件的方法

    python處理文本文件實(shí)現(xiàn)生成指定格式文件的方法

    這篇文章主要介紹了python處理文本文件實(shí)現(xiàn)生成指定格式文件的方法,有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-07-07
  • Python Traceback異常代碼排錯(cuò)利器使用指南

    Python Traceback異常代碼排錯(cuò)利器使用指南

    這篇文章主要為大家介紹了Python Traceback異常代碼排錯(cuò)利器使用指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • python實(shí)現(xiàn)圖像隨機(jī)裁剪的示例代碼

    python實(shí)現(xiàn)圖像隨機(jī)裁剪的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)圖像隨機(jī)裁剪的示例代碼,幫助大家更好的理解和使用python處理圖片,感興趣的朋友可以了解下
    2020-12-12

最新評(píng)論