Mongodb基本操作與Python連接mongodb并進行基礎操作的方法
mongodb是基于分布式文件存儲的nosql(非關系型)數(shù)據(jù)庫
雖說是nosqldb, but mongodb 其中的文檔可以是關系型的
在mongodb中, 表為集合, 里面的數(shù)據(jù)為文檔; 文檔本質就是一條調JSON數(shù)據(jù)
進入mongodb: mongo
退出mongodb: exit
庫操作
顯示庫: show dbs
選擇或者創(chuàng)建: use llsdb
# 不管該庫是否存在,都會use進入, 如何該庫不存在, use進去不存入數(shù)據(jù)退出時,該庫 不會被創(chuàng)建. 所以創(chuàng)建庫就use再寫入數(shù)據(jù).
查看所在庫: db
刪除庫: db.dropDatabase() ,要先use進入庫才刪除.
集合操作
顯示集合: 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'}是條件, 用于匹配項來更新.
指定字段更新 $set,{multi: true}: db.llscol.update({name: 'lls1'}, {$set: {name: 'xxx', age: 666}})
# 前面的{}為條件, 只會更新匹配到的第一個項.
更新多條 {multi: true}: db.llscol.update({name: 'lls1'}, {$set: {name: 'lls666'}}, {multi: true})
# 在全文檔匹配到對應項后更新每一條JSON中的對應的鍵值對.
刪除數(shù)據(jù): db.llscol.remove({name: 'lls1'})
# remove 所以包含{name: ‘lls1'}的json.
只刪除一條 {justOne: true}: db.llscol.remove({name: 'lls1'}, {justOne: true})
# 刪除匹配到的第一項.
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'})) # 查詢無args的第一行,或匹配到的第一個行.
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的基礎方法非?;A但也是日常工作中的必知必會的知識點,如果你想了解更多Mongodb基本操作的相關內容請查看下面相關鏈接
- python連接mongodb操作數(shù)據(jù)示例(mongodb數(shù)據(jù)庫配置類)
- Python中的MongoDB基本操作:連接、查詢實例
- python連接mongodb密碼認證實例
- python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫的方法
- Python簡單連接MongoDB數(shù)據(jù)庫的方法
- python實現(xiàn)連接mongodb的方法
- python連接mongodb集群方法詳解
- python連接、操作mongodb數(shù)據(jù)庫的方法實例詳解
- python連接mongodb數(shù)據(jù)庫操作數(shù)據(jù)示例
- Python如何使用pymongo連接MongoDB數(shù)據(jù)庫并進行相關操作
相關文章
MongoDB中強大的統(tǒng)計框架Aggregation使用實例解析
這篇文章主要介紹了MongoDB中強大的統(tǒng)計框架Aggregation使用實例解析,文中舉了Python和Java使用Aggregation的數(shù)據(jù)統(tǒng)計例子進行講解,需要的朋友可以參考下2016-01-01
MongoDB增刪查改操作示例【基于JavaScript Shell】
這篇文章主要介紹了MongoDB增刪查改操作,結合實例形式分析了MongoDB數(shù)據(jù)庫基于JavaScript Shell的基本增刪查改操作技巧與使用注意事項,需要的朋友可以參考下2019-07-07
分布式文檔存儲數(shù)據(jù)庫之MongoDB分片集群的問題
這篇文章主要介紹了分布式文檔存儲數(shù)據(jù)庫之MongoDB分片集群的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法
MongoDB是一個跨平臺,面向文檔的數(shù)據(jù)庫,提供高性能,高可用性和易于擴展。MongoDB是工作在集合和文檔上一種概念。下面通過本文給大家分享Centos7安裝和卸載Mongodb數(shù)據(jù)庫的方法,需要的朋友參考下吧2017-11-11
MongoDB使用mongoexport和mongoimport命令,批量導出和導入JSON數(shù)據(jù)到同一張表的實例
今天小編就為大家分享一篇關于MongoDB使用mongoexport和mongoimport命令,批量導出和導入JSON數(shù)據(jù)到同一張表的實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
MongoDB 3.4 安裝以 Windows 服務方式運行的詳細步驟
這篇文章主要介紹了MongoDB 3.4 安裝以 Windows 服務方式運行的詳細步驟,需要的朋友可以參考下2017-09-09

