mongodb 簡單入門
- 一、安裝
- 二、資料庫
- 三、文檔
-
- 1、基本查詢
- 2、範圍查詢
- 3、複雜查詢
- 4、抽出字段
- 5、文檔方法
- 6、文檔更新
- 7、更多相關的函數
- 四、索引
一、安裝
參考菜鳥教程:點選跳轉
二、資料庫
- 建立資料庫
(建立成功會直接切換到這個資料庫)use [資料庫名]
- 目前庫資訊
db.stats()
- 建立集合
例如:db.createCollection("[集合名]")
db.createCollection("user")
- 檢視目前資料庫中所有集合
show collections
- 删除表
例如:db.[資料庫名].drop()
db.user.drop()
- 删除資料庫
例如:db.dropDatabase("[資料庫名]")
db.dropDatabase("test")
三、文檔
這裡預設建立集合
db.createCollection("user")
=>
user
表
1、基本查詢
- 表插入資料:
db.user.insert( { id: 1, name: "zhangsan", age: 10 } );
- 檢視所有資料
db.user.find()
- 查找指定條件資料
查找 id=2, name=“zs” 的這條資料db.user.find({id:2, name:"zs"})
- 循環插入
for(var i=5; i<10; i++){ db.user.insert({id:i, name:"t"+i, age:20+i}) }
- 文檔資料條數
db.user.count()
- 删除資料
//删除 id = 1 的資料 db.user.remove({id:1}) // 删除所有資料 db.user.remove({})
2、範圍查詢
大于等于 $gte
大于 $gt
小于等于 $lte
小于 $lt
等于 $eq
不等于 $nq
正則 /k/、/^k/
- 大于小于查詢
// id >= 6
db.user.find({id:{$gte: 6}})
// id > 6
db.user.find({id:{$gt: 6}})
// 正則,查找 name 是測試開頭的記錄
db.user.find({name:/^測試/})
// 去重某一字段 [ 25, 26, 27, 28, 29, 29 ] => [ 25, 26, 27, 28, 29 ]
db.user.distinct("age")
3、複雜查詢
或 $or
範圍 $in
存在 $exists
- $exists
// 查詢 age 字段不存在的記錄 { "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "測試測試測試" } db.user.find({age: {$exists: false}}) // 查詢 age 字段存在的記錄 { "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 } db.user.find({age: {$exists: true}})
- $in
// age 範圍在 26 ~ 28 之間的資料 db.user.find({ age: {$in: [26, 28]} })
- $or
// id=5 或 name 已測字開頭的 db.user.find({ $or: [{id:5},{name:/^測/}] })
4、抽出字段
相當于關系資料庫的文法:
select *
,true / 1 都指代抽出,false / 0 不抽出
db.user.find({}, {filed:1, filed:true})
// 抽出 name
db.user.find({ $or: [{id:5},{name:/^u/}] }, {name: true, _id: false})
// 抽出之前:{ "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 }
// 抽出之後:{ "name" : "t5" }
// 指定不抽出字段(指定字段不抽出,其他都抽出)
db.user.find({ $or: [{id:5},{name:/^u/}] }, {name: 0, _id: 0})
// 原先:{ "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 }
// 輸出:{ "id" : 5, "age" : 25 }
5、文檔方法
排序:sort()
分頁:limit()
跳過:skip()
資料集合:
> db.user.find() { "_id" : ObjectId("61c978d2c6a78592b954dd5d"), "id" : 5, "name" : "t5", "age" : 25 } { "_id" : ObjectId("61c978d2c6a78592b954dd5e"), "id" : 6, "name" : "t6", "age" : 26 } { "_id" : ObjectId("61c978d2c6a78592b954dd5f"), "id" : 7, "name" : "t7", "age" : 27 } { "_id" : ObjectId("61c978d2c6a78592b954dd60"), "id" : 8, "name" : "t8", "age" : 28 } { "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 } { "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "測試測試測試" } { "_id" : ObjectId("61c97ab8c6a78592b954dd63"), "id" : 11, "name" : "測試測試測試", "age" : 29 }
- sort()
// 按照 id 倒序 db.user.find().sort({id: -1}) // 按照 id 正序 db.user.find().sort({id: 1})
- limit()
// 截取前兩位 db.user.find().limit(2) // 輸出:{ "_id" : ObjectId("61c97ab8c6a78592b954dd63"), "id" : 11, "name" : "測試測試測試", "age" : 29 } // { "_id" : ObjectId("61c97a62c6a78592b954dd62"), "id" : 11, "name" : "測試測試測試"
- skip()
// 跳過前兩位,再截取兩位 db.user.find().sort({id: -1}).skip(2).limit(2) // 輸出:{ "_id" : ObjectId("61c978d2c6a78592b954dd61"), "id" : 9, "name" : "t9", "age" : 29 } // { "_id" : ObjectId("61c978d2c6a78592b954dd60"), "id" : 8, "name" : "t8", "age" : 28 }
6、文檔更新
- update
update([filter], [update], [options])
參數1:過濾參數
參數2:更新資料
參數3:更多選項,可選全部更新等操作
// 更新 id=11 所有資料中的第一條資料, 更新為 id=12
db.user.update( {id: 11}, { $set: {id:12} })
// 更新 id=11 所有資料中 age exists 的第一條資料, 更新為 id=12
db.user.update( {id: 11, age: {$exists:true}}, { $set: {id:12} })
// 更新 id=11 所有資料, 更新為 id=12 (multi: true 更新所有資料)
db.user.update( {id: 11}, { $set: {id:12} }, {multi: true})
- upsert
更新、如沒有這條資料就插入一條 db.user.update( {id: 11}, { $set: {id:12} }, {upsert: true})
7、更多相關的函數
點選跳轉
四、索引
db.collection.createIndex(keys, options)
- 單列索引
1 為指定按升序建立索引,如果你想按降序來建立索引指定為 -1 即可db.user.createIndex({"id":1})
- 複合索引
( id: 升序;age: 降序 )db.user.createIndex({"id":1,"age":-1})
-
options 建立索引選項
options 可選參數點選跳轉
例如:通過在建立索引時加 background:true 的選項,讓建立工作在背景執行
db.values.createIndex({open: 1, close: 1}, {background: true})
- 擷取文檔索引資訊
db.user.getIndexes()
【MongoDB】mongodb 簡單入門一、安裝二、資料庫三、文檔四、索引