09 mongoDB基礎(進階)

來源:互聯網
上載者:User

標籤:collect   multi   集合   返回   create   sql   格式   退出   upd   

mongoDB基礎階段一、認識mongodb1、mongodb 組織資料的基本形式

MongoDB————》資料庫————》集合————》文檔

mysql:表:行和列:欄位

運用情境:如 送外賣騎手的即時資訊、日誌、遊戲人物屬性、附近人資訊

運用特徵:即時變化的 QPS

資料類型:每個文檔以索引值對的形式儲存

2、進入與退出 資料庫

進入:mongo

退出:exit

階段二、庫,集合操作

1、顯示所有庫:shou dbs

2、切換資料庫:use 資料庫名稱

3、查看所在庫:db

4、刪除庫:db.dropDatabase()

5、查看當前庫裡面的集合:show collections

6、建立集合:db.createCollection(‘student‘)

7、刪除集合:db.集合名稱.drop()

階段三、資料的增刪改查操作1、插入資料

db.集合名稱.insert(document)

每一條資料,就是一個document,即 就是一條json

插入文檔時,如果不指定_id參數,MongoDB會為文檔分配一個唯一的ObjectId

# 例:插入一條db.student.insert({name:‘shiwei‘,age:18})# 插入時加入iddb.student.insert({_id:10,name:‘shiwei‘,age:18})?# 插入多條:db.student.insert([    {name:‘shiwei‘,sex:‘male‘,age:18},    {name:‘張三‘,sex:’female‘,age:30 },    {name:‘李四‘,sex:’male‘,age:48 },])
2、查詢資料

db.集合名稱.find()

# 查詢所有:db.student.find()# 查詢所有、格式會縮排:db.student.find().pretty()# 按條件查詢:> db.student.find({name:‘zcm‘}){ "_id" : ObjectId("5b3789beccd791f53ba27b05"), "name" : "zcm", "age" : 22 }{ "_id" : ObjectId("5b378bc6ccd791f53ba27b07"), "name" : "zcm", "age" : 22 }?> db.student.find({name:‘zcm‘},{age:1}){ "_id" : ObjectId("5b3789beccd791f53ba27b05"), "age" : 22 }{ "_id" : ObjectId("5b378bc6ccd791f53ba27b07"), "age" : 22 }?> db.student.find({name:‘zcm‘},{age:0}){ "_id" : ObjectId("5b3789beccd791f53ba27b05"), "name" : "zcm" }{ "_id" : ObjectId("5b378bc6ccd791f53ba27b07"), "name" : "zcm" }
3、更新資料

更新資料

db.集合名稱.update(

<query>, (條件)

<update>,

{multi: <boolean>}

? )

# 全文檔更新:db.stu.update({name:’shiwei’},{xx:’yy’ } )?# 指定屬性更新,通過操作符$set,有相同的屬性時,只更改一條資料db.student.update({name:‘zcm‘},{$set:{age:18}})?# 更新多條: { multi: ture }   。 (前提只能指定欄位更新)db.student.update({name:‘zcm‘},{$set:{age:5}},{multi:true})
4、刪除資料

db.集合名稱. remove(

? <query>, (條件)

<justOne>

? )

# 只是刪除 一條資料db.student.remove({xx:‘yy‘},{justOne:true})?# 刪除滿足條件的,所有資料:db.student.remove({name:‘zcm‘})
階段四、Python操作mongodb1、連接器(驅動)

安裝python包:pip install pymongo

引入包pymongo:import pymongo

建立串連並建立用戶端: client= pymongo.MongoClient(‘127.0.0.1‘,27017)

指定資料庫:db=client[ 資料庫名 ]

指定集合:stu=db [ 集合名]

2、主要方法

insert_one

insert_many

update_one

update_many

delete_one

delete_many

find_one

find

3、例子
import pymongo?# 建立串連client = pymongo.MongoClient(‘127.0.0.1‘, 27017)?# 擷取要操作的資料庫db = client[‘student‘]?# 擷取要操作的集合col = db[‘student‘]?data = col.find()print(data)  # 列印出了一個對象,證明串連成功了?# 插入一條資料# col.insert_one({‘name‘: ‘zcm‘, ‘age‘: 22})?# 插入多條資料# li = [#     {‘nane‘: ‘zhangsan‘, ‘age‘: 30, ‘sex‘: ‘nan‘},#     {‘nane‘: ‘baby‘, ‘age‘: 18, ‘sex‘: ‘nv‘},#     {‘nane‘: ‘dengchao‘, ‘age‘: 30, ‘sex‘: ‘nan‘}# ]# col.insert_many(li)?# 更新一條資料# col.update_one({‘name‘: ‘zcm‘}, {‘$set‘: {‘age‘: 3}})?# 更新多條資料# col.update_many({‘name‘:‘zcm‘},{‘$set‘: {‘age‘:18}})?# 查詢一條資料# find = col.find_one()# print(find)?# 查詢多條資料    只返回對象# find_d = col.find()# print(find_d)?# 刪除一條資料# col.delete_one({‘name‘:‘zcm‘},{‘justOne‘:‘true‘})?# 刪除多條資料# col.delete_many({‘name‘:‘zcm‘})

 

09 mongoDB基礎(進階)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.