3.2 打開數據庫連接池
mongod --dbpath d:\data\db
// 如果報錯
./mongod --dbpath d:\data\db
如果還不可用,就用管理員身份去運行
最后還不可用,換電腦或者換系統
如果輸出 waiting for connections on port 27017 表明連接池打開成功
3.3 打開命令行的數據庫客戶端
打開壓縮的mongodb文件夾,進入bin目錄
shift + 右鍵 選擇打開 命令行窗口
3.4 數據庫常用命令
help 查看幫助文檔
db.help() 數據庫的幫助文檔
db.test.help() 當前數據庫下test集合的幫助文檔
db.test.find().help() 當前數據庫下test集合的查詢的幫助文檔
show dbs 查詢當前數據庫連接池中的所有的數據庫
admin 0.000GB
local 0.000GB
use sh1908 無則創建並且切換,有則切換
switched to db sh1908
db 查看當前是哪一個數據庫
sh1908
db.stats() 當前數據庫狀態
{
"db" : "sh1908",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"ok" : 1
}
db.version() 查詢當前數據庫的版本
3.4.2
db.getMongo() 查看當前DB的鏈接機器地址
connection to 127.0.0.1:27017
db.dropDatabase() 刪除數據庫
{ "ok" : 1 }
show dbs 如果數據庫沒有數據,那么不會顯示該數據庫
4、collection 聚集集合操作
4.1、創建集合
db.createCollection('users') 創建了用戶集合--創建了用戶表
{ "ok" : 1 }
show dbs 此時可以觀察到有了sh1908的數據庫
語法:db.createCollection('users', {size: 20, capped: true, max: 100})
創建了集合users,創建的集合是固定的(capped: true,必須size配合的參數,如果達到最大值,會自動覆蓋最先的數據),最大的容量為20字節(size:20,單位為字節),最多存儲100條數據(max:100)
字段 | 類型 | 描述 |
---|---|---|
capped | 布爾(可選) | 如果為 true,則創建固定集合。固定集合是指有着固定大小的集合,當達到最大值時,它會自動覆蓋最早的文檔。當該值為 true 時,必須指定 size 參數。 |
autoIndexId | 布爾(可選) | 如為 true,自動在 _id 字段創建索引。默認為 false。 |
size | 數值(可選) | 為固定集合指定一個最大值(以字節計)。如果 capped 為 true,也需要指定該字段。 |
max | 數值(可選) | 指定固定集合中包含文檔的最大數量。 |
db.createCollection('course')
{ "ok" : 1 }
4.2 得到指定名稱的聚集集合
db.getCollection('user')
sh1908.user
4.3 得到當前DB的所有的聚集集合
db.getCollectionNames()
[ "course", "users" ]
4.4 顯示當前db所有集合的狀態
db.printCollectionStats()
5、document文檔操作
增刪改查
5.1 插入操作 ----- 增
db.course
sh1908.course
db.course.insert({week1: 'node', week2: 'vue', week5: 'react+混合開發', week8:'微信相關開發', week9: '復習'})
WriteResult({ "nInserted" : 1 })
插入單條數據
db.col.insert({})
db.col.insertOne({})
插入多條數據
db.col.insert([ {}, {}, {} ])
db.col.insertMany([ {}, {}, {} ])
了解
插入文檔你也可以使用 db.col.save(document) 命令。如果不指定 _id 字段 save() 方法類似於 insert() 方法。如果指定 _id 字段,則會更新該 _id 的數據。
db.users.insertOne({username: '吳大勛', password: '123456', sex: 1, age: 18, lesson: 3, city: '山西'})
{
"acknowledged" : true,
"insertedId" : ObjectId("5da5690eee7de50b8cbc6ea5")
}
db.users.insertMany([{username: '徐石坡', password:'123456', sex: 1, age: 25, lesson: 3, city: '安徽'},{username: '操鑫', password:'123456', sex: 1, age: 40, lesson: 3, city: '安徽'}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5da56a10ee7de50b8cbc6ea6"),
ObjectId("5da56a10ee7de50b8cbc6ea7")
]
}
5.2 簡單查詢插入的數據 ---- 看一下數據是什么
db.users.find() 查詢當前數據庫集合下的所有數據
{ "_id" : ObjectId("5da5690eee7de50b8cbc6ea5"), "username" : "吳大勛", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"), "username" : "徐石坡", "password" : "123456", "sex" : 1, "age" : 25, "lesson" : 3, "city" : "安徽" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"), "username" : "操鑫", "password" : "123456", "sex" : 1, "age" : 40, "lesson" : 3, "city" : "安徽" }
db.users.find().pretty() 查詢數據並且格式化
{
"_id" : ObjectId("5da5690eee7de50b8cbc6ea5"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西"
}
{
"_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽"
}
{
"_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 40,
"lesson" : 3,
"city" : "安徽"
}
5.3 刪除數據
db.users.deleteOne({username: '吳大勛'}) 刪除用戶名為吳大勛的一條記錄
{ "acknowledged" : true, "deletedCount" : 1 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽"
}
{
"_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 40,
"lesson" : 3,
"city" : "安徽"
}
db.users.deleteMany({age: 40}) 刪除多條年齡為40歲的記錄
{ "acknowledged" : true, "deletedCount" : 1 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽"
}
db.users.deleteMany({}) 刪除所有的數據
{ "acknowledged" : true, "deletedCount" : 1 }
db.users.find().pretty()
// 沒有返回
小結
- db.col.deleteOne({key: value}) 刪除單條數據
- db.col.deleteMany({key: value}) 刪除多條數據
- db.col.deleteMany({}) 刪除所有的數據
5.4 修改數據
依據5.1步驟插入數據
db.users.find().pretty()
{ 西'})
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 40,
"lesson" : 3,
"city" : "安徽"
}
db.users.updateOne({username: '吳大勛'}, { $set: {age: 20}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 20,
"lesson" : 3,
"city" : "山西"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 40,
"lesson" : 3,
"city" : "安徽"
}
db.users.updateMany({}, { $set: { company: '千鋒'}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 20,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 25,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 40,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
db.users.updateMany({}, { $inc: {age: 1}}) // 所有的年齡 +1
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 21,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 26,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 41,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
db.users.updateMany({}, { $inc: {age: -3}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
db.users.find().pretty()
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
"username" : "徐石坡",
"password" : "123456",
"sex" : 1,
"age" : 23,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
{
"_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
"username" : "操鑫",
"password" : "123456",
"sex" : 1,
"age" : 38,
"lesson" : 3,
"city" : "安徽",
"company" : "千鋒"
}
小結
- db.col.updateOne({key:value}, {$set: {key1: value1}})
- db.col.updateOne({key:value}, {$inc: {key1: 1}})
- db.col.updateMany({key:value}, {$set: {key1: value1}})
- db.col.updateMany({key:value}, {$inc: {key1: 1}})
- db.col.updateMany({}, {$set: {key1: value1}})
- db.col.updateMany({}, {$inc: {key1: 1}})
5.5 查詢數據
db.users.find().pretty()
db.users.find({},{}).pretty() // 同上,第一個{}代表查詢的條件,第二個代表顯示的字段
db.users.find({username:'吳大勛'}).pretty() // 查詢用戶名為吳大勛的記錄
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
db.users.find({username:'吳大勛'}, {username: 1, age: 1}).pretty()
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"age" : 18
}
db.users.find({username:'吳大勛'}, {username: 1, age: 1, _id: 0}).pretty()
{ "username" : "吳大勛", "age" : 18 }
db.users.find({}, {username: 1, age: 1, _id: 0}).pretty()
{ "username" : "吳大勛", "age" : 18 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "操鑫", "age" : 38 }
db.users.find({ age: { $gte: 30} }, {_id:0, username: 1}).pretty() // 查詢大於等於30歲的數據 大於用 $gt
{ "username" : "操鑫" }
db.users.find({ age: { $gte: 18, $lte: 30} }, {_id:0, username: 1}).pretty()
{ "username" : "吳大勛" }
{ "username" : "徐石坡" }
db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: 1}).pretty() // 年齡的升序
{ "username" : "吳大勛", "age" : 18 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "操鑫", "age" : 38 }
db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: -1}).pretty() // 年齡的降序
{ "username" : "操鑫", "age" : 38 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "吳大勛", "age" : 18 }
db.users.find({username: /大/}, {}).pretty() // 查詢名字中帶大的數據---模糊查詢
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
db.users.find({username: /^吳/}, {}).pretty() // 查詢名字中帶吳且開頭的數據---模糊查詢
{
"_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
"username" : "吳大勛",
"password" : "123456",
"sex" : 1,
"age" : 18,
"lesson" : 3,
"city" : "山西",
"company" : "千鋒"
}
db.users.distinct('city') // 查詢所有數據的city字段組成數組,並且去重
[ "山西", "安徽" ]
db.users.distinct('age')
[ 18, 23, 38 ]
db.users.find().count() // 查詢的條數
3
- 想要查看其他的查詢方法 db.col.find().help()
find(<predicate>, <projection>) modifiers
.sort({...})
.limit(<n>) ************************** 分頁
.skip(<n>) ************************** 分頁
.batchSize(<n>) - sets the number of docs to return per getMore
.collation({...})
.hint({...})
.readConcern(<level>)
.readPref(<mode>, <tagset>)
.count(<applySkipLimit>) - total # of objects matching query. by default ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain(<verbosity>) - accepted verbosities are {'queryPlanner', 'executionStats', 'allPlansExecution'}
.min({...})
.max({...})
.maxScan(<n>)
.maxTimeMS(<n>)
.comment(<comment>)
.snapshot()
.tailable(<isAwaitData>)
.noCursorTimeout()
.allowPartialResults()
.returnKey()
.showRecordId() - adds a $recordId field to each returned object
Cursor methods
.toArray() - ************************************ 將每一條記錄轉換成數組
.forEach(<func>)
.map(<func>)
.hasNext()
.next()
.close()
.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
.itcount() - iterates through documents and counts them
.getQueryPlan() - get query plans associated with shape. To get more info on query plans, call getQueryPlan().help().
.pretty() - pretty print each document, possibly over multiple lines
小結
- db.col.find()
- db.col.find().pretty()
- db.col.find({key: value})
- db.col.find({}, {_id:0, key1: 1})
- db.col.find({price: { $gte: 100, $lte: 200}})
- db.col.find().count()
- db.col.distinct('key')
- db.col.find().toArray() // 轉換成數組
- db.col.find().limit(num) // 只能查詢num條數據
- db.col.find().skip(n) // 從第n條數據開始查詢,下標從0開始
- db.col.find($or: [{age:18, lesson:2}]) // 查詢年齡為18 或者 二階段的數據