最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、安裝

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})

八、高級查詢

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)計

import pymongo

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

# 統(tǒng)計集合中總共有多少條數(shù)據(jù)
collections.find().count()
# 統(tǒng)計集合中年齡大于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

# 對字段 age 按升序排序
collections.find().sort('age')
# 對字段 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中序列的修改、散列與切片詳解

    Python中序列的修改、散列與切片詳解

    在Python中,最基本的數(shù)據(jù)結(jié)構(gòu)是序列(sequence)。下面這篇文章主要給大家介紹了關(guān)于Python中序列的修改、散列與切片的相關(guān)資料文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考,下面來一起看看吧。
    2017-08-08
  • 使用Python制作一個簡易的遠(yuǎn)控終端

    使用Python制作一個簡易的遠(yuǎn)控終端

    這篇文章主要為大家詳細(xì)介紹了如何使用Python語言制作一個簡易的遠(yuǎn)控終端,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的可以了解一下
    2023-04-04
  • Python基礎(chǔ)之numpy庫的使用

    Python基礎(chǔ)之numpy庫的使用

    這篇文章主要介紹了Python基礎(chǔ)之numpy庫的使用,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 解決python字典對值(值為列表)賦值出現(xiàn)重復(fù)的問題

    解決python字典對值(值為列表)賦值出現(xiàn)重復(fù)的問題

    今天小編就為大家分享一篇解決python字典對值(值為列表)賦值出現(xiàn)重復(fù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python圖像處理之鏡像實現(xiàn)方法

    python圖像處理之鏡像實現(xiàn)方法

    這篇文章主要介紹了python圖像處理之鏡像實現(xiàn)方法,實例分析了鏡像的實現(xiàn)原理與具體操作方法,需要的朋友可以參考下
    2015-05-05
  • 詳解解決Python memory error的問題(四種解決方案)

    詳解解決Python memory error的問題(四種解決方案)

    這篇文章主要介紹了詳解解決Python memory error的問題(四種解決方案),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python采集某度貼吧排行榜實戰(zhàn)示例

    Python采集某度貼吧排行榜實戰(zhàn)示例

    這篇文章主要為大家介紹了Python采集某度貼吧排行榜實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 使用python 對驗證碼圖片進(jìn)行降噪處理

    使用python 對驗證碼圖片進(jìn)行降噪處理

    今天小編就為大家分享一篇使用python 對驗證碼圖片進(jìn)行降噪處理,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python中urllib模塊用法實例詳解

    python中urllib模塊用法實例詳解

    這篇文章主要介紹了python中urllib模塊用法,以實例形式詳細(xì)分析了python中urllib模塊代替PHP的curl操作方法,具有不錯的借鑒價值,需要的朋友可以參考下
    2014-11-11
  • python安裝whl文件的實戰(zhàn)步驟

    python安裝whl文件的實戰(zhàn)步驟

    whl格式本質(zhì)上是一個壓縮包,里面包含了py文件,以及經(jīng)過編譯的pyd文件,下面這篇文章主要給大家介紹了關(guān)于python安裝whl文件的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07

最新評論

西华县| 郁南县| 海伦市| 温宿县| 盐亭县| 石阡县| 卫辉市| 盐亭县| 林甸县| 高平市| 西乌| 冕宁县| 新兴县| 从化市| 兰州市| 贵州省| 剑河县| 锡林浩特市| 萝北县| 改则县| 政和县| 惠东县| 湖南省| 益阳市| 泸定县| 阿克苏市| 龙海市| 溆浦县| 阳信县| 建水县| 彭州市| 富顺县| 临安市| 津南区| 霸州市| 苍溪县| 浦城县| 临潭县| 江门市| 镶黄旗| 雷州市|