wordpress去水印北京seo公司華網(wǎng)白帽
MongoDB Shell 基本命令(一)
1. 基本概念
SQL術(shù)語/概念 | MongoDB術(shù)語/概念 | 解釋/說明 |
---|---|---|
database | db | 數(shù)據(jù)庫 |
table | collection | 數(shù)據(jù)庫表/集合 |
row | document | 數(shù)據(jù)記錄行/文檔 |
column | field | 數(shù)據(jù)字段/域 |
index | index | 索引 |
table joins | 表連接,MongoDB不支持 | |
primary key | primary key | 主鍵,MongoDB自動將_id字段設(shè)置為主鍵 |
2. 文檔的基本數(shù)據(jù)結(jié)構(gòu)
-
文檔的基本結(jié)構(gòu):{ : , : {}, … }
-
封閉符 {}
-
分隔符 ,
-
連接符 :
-
鍵的數(shù)據(jù)類型:UTF-8字符,可以用”“引起來,如”name“
-
的用戶命名規(guī)則:
(1)‘_id’ 為保留字段key
(2) 禁止使用’$'符號
(3) 禁止使用’.'符號
(4) 避免同一個{}中使用重復(fù)的
-
-
值的數(shù)據(jù)類型:MongoDB支持的任意數(shù)據(jù)類型
示例一:
{ "_id" : 1, "name" : "Zhang San" }
示例二:
{ "_id" : ObjectId("5e746c62040a548ab32fff13"), //ObjectId對象"name" : "Zhang San", // 字符串"age" : 18, // 數(shù)字(Double)"alive" : true, // 布爾值"hobbies" : ["Anime","Comic","Game", 19, false,[1,2,3],{a:1}], // 數(shù)組"body": {"height" : 170,"weight" : 65}, // Object 內(nèi)嵌文檔"courses" : [{ "coursename" : "nosql" },{ "coursename" : "mysql" },{ "coursename" : "python" },{ "coursename" : "linux" },{ "coursename" : "kettle" }] // 內(nèi)嵌文檔的數(shù)組 }
-
-
MongoDB的基本數(shù)據(jù)類型
數(shù)據(jù)類型 | 描述 |
---|---|
String | 字符串。存儲數(shù)據(jù)常用的數(shù)據(jù)類型。在 MongoDB 中,UTF-8 編碼的字符串才是合法的。 |
Integer | 整型數(shù)值。用于存儲數(shù)值。根據(jù)你所采用的服務(wù)器,可分為 32 位或 64 位。 |
Boolean | 布爾值。用于存儲布爾值(真/假)。true/false = 1/0 |
Double | 雙精度浮點值。用于存儲浮點值。 |
Min/Max keys | 將一個值與 BSON(二進(jìn)制的 JSON)元素的最低值和最高值相對比。 |
Arrays | 用于將數(shù)組或列表或多個值存儲為一個鍵。[] |
Timestamp | 時間戳。記錄文檔修改或添加的具體時間。Timestamp() |
Object | 用于內(nèi)嵌文檔。{} |
Null | 用于創(chuàng)建空值。 |
Symbol | 符號。該數(shù)據(jù)類型基本上等同于字符串類型,但不同的是,它一般用于采用特殊符號類型的語言。 |
Date | 日期時間。用 UNIX 時間格式來存儲當(dāng)前日期或時間。你可以指定自己的日期時間:創(chuàng)建 Date 對象,傳入年月日信息。 |
Object ID | 對象 ID。用于創(chuàng)建文檔的 ID。 |
Binary Data | 二進(jìn)制數(shù)據(jù)。用于存儲二進(jìn)制數(shù)據(jù)。 |
Code | 代碼類型。用于在文檔中存儲 JavaScript 代碼。 |
Regular expression | 正則表達(dá)式類型。用于存儲正則表達(dá)式。 |
3. 數(shù)據(jù)庫(db)
// 查看當(dāng)前服務(wù)器上的數(shù)據(jù)庫
show dbs;
show databases;// 選擇名為mydb的數(shù)據(jù)庫(如果沒有則隱式創(chuàng)建)
use mydb;// 查看當(dāng)前使用的數(shù)據(jù)庫
db;// 查看當(dāng)前數(shù)據(jù)庫的統(tǒng)計信息,由于沒有插入相關(guān)信息,基本上為空值
db.stats();// 查看當(dāng)前數(shù)據(jù)庫的操作信息,返回當(dāng)前正在執(zhí)行的操作
db.currentOp();// 刪除當(dāng)前數(shù)據(jù)庫
db.dropDatabase();
4. 集合(collection)
// 查看當(dāng)前數(shù)據(jù)庫中的集合
show collections;
show tables;// 創(chuàng)建一個名為mycoll的集合
db.createCollection("mycoll");// 重命名mycoll集合,新集合名叫mycollection
db.mycoll.renameCollection("mycollectioin")// 清空一個名為mycollection的集合
db.mycollection.remove({});// 刪除一個名為mycollection的集合 如果數(shù)據(jù)庫沒有任何集合時,數(shù)據(jù)庫會被自動刪除。
db.mycollection.drop();
4.1 添加文檔到集合
insert() 方法
注意:db.collection中,collection為你要操作的集合的名稱
db.collection.insert(<document or array of documents>,{multi: false}
)
insertOne() 方法
添加一條文檔記錄
db.collection.insertOne(<document>{}
)
insertMany() 方法
添加多條文檔記錄 ([]方括號表示數(shù)組)
db.collection.insertMany([ <document 1> {} , <document 2> {}, ... ] --jsonArray
)
添加一條文檔記錄{“l(fā)astname”:“zhang”, “firstname”:“ren”}到集合mycollection
db.mycollection.insert({"lastname":"zhang", "firstname":"san"});
//返回id,比較好用,直接得到結(jié)果,進(jìn)一步獲取到ObjectID
db.mycollection.insertOne({"lastname":"zhang", "firstname":"san"});
var result = db.mycollection.insertOne({"lastname":"zhang", "firstname":"san"});
var newdoc_id = result.insertedId
//可以通過find()命令查找,如果不使用bson格式會報什么錯誤?
db.mycollection.find({_id:newdoc_id})//通過id找到對應(yīng)信息
添加一個文檔記錄對象mydoc到集合mycollection, 使用insert或insertOne方法
var mydoc = {"lastname":"Zhang", "firstname":"San"};
db.mycollection.insert(mydoc);
// 3.2版后新的方法:insertOne
db.mycollection.insertOne(mydoc);
添加多條記錄到集合mycollection,使用insert或insertMany方法
// 多條文檔記錄,用數(shù)組[]進(jìn)行組合,用逗號分隔
db.mycollection.insert(
[{"lastname":"Zhang", "firstname":"san", "role":"teacher", "teacher_id":"2023409", "title":"教師", "courses":[{"coursename":"nosql"},{"coursename":"sqlserver"}]},{"lastname":"Zhang", "firstname":"San", "role":"student", "student_id":"2022000000", "grade":"2022", "class":"1", "score":80}
]
);
添加一個文檔數(shù)組mydocs(多條文檔的數(shù)組)到集合mycollection,使用insert或insertMany方法
// 多條文檔記錄用[]組合到一個數(shù)組mydocs中。
// 注意 coursename處于兩個花括號中,屬于兩個內(nèi)嵌的文檔,不算重復(fù)的鍵
var mydocs = [{"lastname" : "Zhang","firstname" : "San","role" : "teacher","teacher_id" : "2023409","title" : "教師","courses" : [{ "coursename" : "nosql" },{ "coursename" : "mysql" },{ "coursename" : "python" },{ "coursename" : "linux" },{ "coursename" : "kettle" }]},{"lastname" : "Zhang","firstname" : "Yi","role" : "student","student_id" : "2022000001","grade" : "2022","class" : "1","score" : 80},{"lastname" : "Tan","firstname" : "Er","role" : "student","student_id" : "2022000002","grade" : "2022","class" : "1","score" : 70}
];
db.mycollection.insertMany(mydocs);
// 3.2版后新的方法:insertMany
4.2 查詢文檔記錄
find() 方法
db.<collection>.find(<querydocument>, <projection>)
db.<collection>.findOne(<querydocument>, <projection>)select <projection> from <collection> where <querydocument>
select student_id from mycollection where score > 70;
db.mycollection.find({"score":{$gt:70}},{student_id:1});
// 查詢集合mycollection中的文檔
db.mycollection.find();// 將查詢結(jié)果"漂亮化",只在終端有用
db.mycollection.find().pretty();// 查詢集合mycollection中鍵為role, 值為student的文檔記錄
db.mycollection.find( {"role" : "student"} );// 將查詢條件寫入文檔對象ceriteria查詢
var criteria = { "role" : "student" };
db.mycollection.find(criteria);// 使用內(nèi)嵌對象的字段值查詢
db.mycollection.find({"courses.coursename":"mysql"})
匯總操作
//1.創(chuàng)建數(shù)據(jù)庫
//use 使用數(shù)據(jù)庫,如果這個數(shù)據(jù)庫不存在就創(chuàng)建
use cqust
db = db.getSiblingDB("cqust")
//Note:剛才創(chuàng)建的數(shù)據(jù)庫看不到,沒有顯示,因為只是創(chuàng)建了一個名字,在創(chuàng)建集合前沒有文件生成
//2.查看數(shù)據(jù)庫名
show databases;
//可以簡寫為:
show dbs;
//3.集合(collection)可以顯示的創(chuàng)建,執(zhí)行完該條語句,便創(chuàng)建了cqust數(shù)據(jù)庫,以及集合
//也可以不顯示創(chuàng)建
db.createCollection("collectionname");
//4.在集合中插入文檔
//數(shù)據(jù)庫操作命令: db.集合名.集合操作(參數(shù)...)
//構(gòu)造文檔JSON 重復(fù)字段自動過濾掉
//db.集合名.insert(文檔對象)
//db.集合名.insert(文檔列表對象)
//構(gòu)造文檔JSON 重復(fù)字段自動過濾掉
var doc={"姓名":"張三",性別:"男",學(xué)號:"2023409",課程:"分布式數(shù)據(jù)庫原理",成績:90,學(xué)號:"2023409"};
db.collectionname.insert(doc)
課堂練習(xí):
1.創(chuàng)建學(xué)號+姓名(拼音)的數(shù)據(jù)庫
2.創(chuàng)建data集合并查看該集合
3.在data集合中插入個人信息,包括:學(xué)號、姓名、性別、家庭地址、今天的所有課程等信息
4.通過find()查看剛才插入的信息,要求參數(shù)是insertedId
5.清空集合,刪除集合,刪除數(shù)據(jù)庫