Mongodb基本操作與Python連接mongodb并進(jìn)行基礎(chǔ)操作的方法
mongodb是基于分布式文件存儲(chǔ)的nosql(非關(guān)系型)數(shù)據(jù)庫(kù)
雖說(shuō)是nosqldb, but mongodb 其中的文檔可以是關(guān)系型的
在mongodb中, 表為集合, 里面的數(shù)據(jù)為文檔; 文檔本質(zhì)就是一條調(diào)JSON數(shù)據(jù)
進(jìn)入mongodb: mongo
退出mongodb: exit
庫(kù)操作
顯示庫(kù): show dbs
選擇或者創(chuàng)建: use llsdb
# 不管該庫(kù)是否存在,都會(huì)use進(jìn)入, 如何該庫(kù)不存在, use進(jìn)去不存入數(shù)據(jù)退出時(shí),該庫(kù) 不會(huì)被創(chuàng)建. 所以創(chuàng)建庫(kù)就use再寫入數(shù)據(jù).
查看所在庫(kù): db
刪除庫(kù): db.dropDatabase() ,要先use進(jìn)入庫(kù)才刪除.
集合操作
顯示集合: show collections
創(chuàng)建集合: db.createCollection('llscol' [,options])
刪除集合: db.llscol.drop()
數(shù)據(jù)的CURD
插入數(shù)據(jù): db.llscol.insert({name: 'lls', age: 18})
插入多條數(shù)據(jù):
db.llscol.insert([
{name: 'lls1', age: 18},
{name: 'lls2', age: 20}
])
查看數(shù)據(jù): db.llscol.find()
帶格式的數(shù)據(jù): db.llscol.find().pretty()
全文檔更新數(shù)據(jù): db.llscol.update({name: 'haha'}, {xx: 'yy'})
# {name: ‘hha'}是條件, 用于匹配項(xiàng)來(lái)更新.
指定字段更新 $set,{multi: true}: db.llscol.update({name: 'lls1'}, {$set: {name: 'xxx', age: 666}})
# 前面的{}為條件, 只會(huì)更新匹配到的第一個(gè)項(xiàng).
更新多條 {multi: true}: db.llscol.update({name: 'lls1'}, {$set: {name: 'lls666'}}, {multi: true})
# 在全文檔匹配到對(duì)應(yīng)項(xiàng)后更新每一條JSON中的對(duì)應(yīng)的鍵值對(duì).
刪除數(shù)據(jù): db.llscol.remove({name: 'lls1'})
# remove 所以包含{name: ‘lls1'}的json.
只刪除一條 {justOne: true}: db.llscol.remove({name: 'lls1'}, {justOne: true})
# 刪除匹配到的第一項(xiàng).
python操作mongodb
import pymongo
client = pymongo.MongoClient('192.168.0.104', 27017) # 連接并建立client.
db = client['llsdb'] # select database_name db = client.llsdb
stu = db['student'] # select table_name stu = db.student
stu.insert_one({'name': 'lls'})
stu.insert_many([
{'name1': 'lls1', 'age': 18},
{'name2': 'lls2', 'age': 20},
{'name3': 'lls3', 'age': 30}
])
stu.update_one({'name1': 'lls1'}, {'$set': {'age': 20}})
stu.update_many({'name2': 'lls2'}, {'$set': {'age': 90}})
print(stu.find_one({'name2': 'lls2'})) # 查詢無(wú)args的第一行,或匹配到的第一個(gè)行.
content_find = stu.find({}) # 空字典表示查詢所有.
print(content_find)
for i in content_find:
print(i)
print(stu.delete_many({'name': 'lls'}).deleted_count)
print(stu.delete_many({}).deleted_count)
"""
方法如下:
insert_one, insert_many, update_one, update_many, delete_one, delete_many, find_one, find.
"""
本文分享Mongodb基本操作與Python連接并操作mongodb的基礎(chǔ)方法非?;A(chǔ)但也是日常工作中的必知必會(huì)的知識(shí)點(diǎn),如果你想了解更多Mongodb基本操作的相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫(kù)配置類)
- Python中的MongoDB基本操作:連接、查詢實(shí)例
- python連接mongodb密碼認(rèn)證實(shí)例
- python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫(kù)的方法
- Python簡(jiǎn)單連接MongoDB數(shù)據(jù)庫(kù)的方法
- python實(shí)現(xiàn)連接mongodb的方法
- python連接mongodb集群方法詳解
- python連接、操作mongodb數(shù)據(jù)庫(kù)的方法實(shí)例詳解
- python連接mongodb數(shù)據(jù)庫(kù)操作數(shù)據(jù)示例
- Python如何使用pymongo連接MongoDB數(shù)據(jù)庫(kù)并進(jìn)行相關(guān)操作
相關(guān)文章
MongoDB中強(qiáng)大的統(tǒng)計(jì)框架Aggregation使用實(shí)例解析
這篇文章主要介紹了MongoDB中強(qiáng)大的統(tǒng)計(jì)框架Aggregation使用實(shí)例解析,文中舉了Python和Java使用Aggregation的數(shù)據(jù)統(tǒng)計(jì)例子進(jìn)行講解,需要的朋友可以參考下2016-01-01
MongoDB增刪查改操作示例【基于JavaScript Shell】
這篇文章主要介紹了MongoDB增刪查改操作,結(jié)合實(shí)例形式分析了MongoDB數(shù)據(jù)庫(kù)基于JavaScript Shell的基本增刪查改操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-07-07
分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB分片集群的問(wèn)題
這篇文章主要介紹了分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB分片集群的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Windows系統(tǒng)安裝運(yùn)行Mongodb服務(wù)
今天小編就為大家分享一篇關(guān)于Windows系統(tǒng)安裝運(yùn)行Mongodb服務(wù),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Centos7安裝和卸載Mongodb數(shù)據(jù)庫(kù)的方法
MongoDB是一個(gè)跨平臺(tái),面向文檔的數(shù)據(jù)庫(kù),提供高性能,高可用性和易于擴(kuò)展。MongoDB是工作在集合和文檔上一種概念。下面通過(guò)本文給大家分享Centos7安裝和卸載Mongodb數(shù)據(jù)庫(kù)的方法,需要的朋友參考下吧2017-11-11
MongoDB使用mongoexport和mongoimport命令,批量導(dǎo)出和導(dǎo)入JSON數(shù)據(jù)到同一張表的實(shí)例
今天小編就為大家分享一篇關(guān)于MongoDB使用mongoexport和mongoimport命令,批量導(dǎo)出和導(dǎo)入JSON數(shù)據(jù)到同一張表的實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
MongoDB 3.4 安裝以 Windows 服務(wù)方式運(yùn)行的詳細(xì)步驟
這篇文章主要介紹了MongoDB 3.4 安裝以 Windows 服務(wù)方式運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下2017-09-09

