MongoDB學習筆記(3)- Mongo Shell 常用查詢命令
本文所使用的MongoDB版本爲 4.0.10
> db.version();
4.0.10
一、find 命令進行簡查詢
find( 查詢條件 ,返回的字段),
1. 查詢時返回所有字段
db.user.find() --> 查詢user集合中所有的數據
> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
db.user.find( {"username":"Mary"} ) --> 列出username=Mary的數據
> db.user.find({"username": "Mary"})
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
db.user.find( {"age":50} ) --> 列出 age = 50 的數據
> db.user.find( {"age":50} )
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
2. 查詢時只返回指定幾個字段
db.user.find({}, {"username":1}) --> 列出所有人的 username 字段
> db.user.find({}, {"username": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }
db.user.find({}, {"tel": 1}) --> 列出所有人的 tel 字段
> db.user.find({}, {"tel": 1})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7") }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8") }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9") }
db.user.find({} , {"age": 0} ) --> 除了age字段,其他字段都列出來
> db.user.find({}, {"age": 0})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart" }
二、常用操作符
1. $lt (<)
查詢年齡小於30歲的用戶
> db.user.find( {"age": { $lt: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
2. $lte (<=)
查詢年齡小於等於30歲的用戶
> db.user.find( {"age": { $lte: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
3. $gt (>)
查詢年齡大於30歲的用戶
> db.user.find( {"age": { $gt: 30 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
4. $gte (>= )
查詢年齡大於等於30歲的用戶
> db.user.find( {"age": { $gte: 30 } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
5. $ne ( <> )
查詢年齡不等於30歲的用戶
> db.user.find( {"age": { $ne: 30 } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
6. $and (AND)
查詢年齡大於10歲且小於40歲的用戶
> db.user.find( { $and: [ { "age": { $lt: 40 } } , {"age": { $gt: 10 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
7. $or (OR)
查詢年齡小於20歲或者大於40歲的用戶
> db.user.find( { $or: [ { "age": { $lt: 20 } } , {"age": { $gt:40 } } ] } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
8. $in (IN)
查詢年齡爲 20, 30, 40 歲的用戶
> db.user.find( { "age" : { $in : [20, 30, 40] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
9. $not (NOT)
查詢年齡不是 20, 40 歲的用戶($not)
> db.user.find( { "age" : { $not: { $in : [20, 40] } } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
10. $nin (NOT IN)
查詢年齡不是 20, 40 歲的用戶
> db.user.find( { "age" : { $nin : [20, 40] } } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
11. $mod (取模)
查詢年齡對 4 取模餘 2 的用戶
> db.user.find( {"age" : { $mod : [4, 2] } } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
12. $exists (存在)
$exists 用於判斷鍵是否
給 Mary 添加 tel 字段,值設爲 null
> db.user.update({ "username": "Mary" }, { $set : { "tel" : null } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
列出存在 tel 字段的記錄
> db.user.find({ "tel" : { $exists : 1 }})
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
判斷值爲 null 時要注意:db.user.find({ "friends" : null })
該命令不僅查出值爲null的記錄,friends 鍵不存在的記錄也會被取出來。
> db.user.find({ "tel" : null })
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
如果想取出 friends 鍵存在,並且值爲 null 的記錄應該這樣來取:
> db.user.find( { $and: [ { "tel" : null }, { "tel" : {$exists: 1 } } ] } )
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
13. $where
$where : 根據函數返回值來判斷是否返回數據
取出 年齡 * 3 + 5 小於 100
用戶的
db.user.find( { $where : function(){
return (this.age * 3 + 5 < 100);
} } );
> db.user.find( { $where : function(){ return (this.age * 3 + 5 < 100); } } );
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086" }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
this
代表當前這個記錄。通過這個 $where 基本可以實現任意類型的查詢。不過不到必要時候不要用這個方法,因爲它的速度比一般查詢要慢很多。
三、使用正則查詢
查找名字中含有 "art" 的記錄
> db.user.find( { "username" : /art/ } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
查找以名稱以 "mar" 開頭且不區分大小寫的記錄
> db.user.find( {"username": /^mar/i } );
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40 }
四、查詢數組
1. $all:判斷某個數組類型字段包含的多個指定值時。
給 user 集合中的用戶添加一些朋友,結果如下:
> db.user.find()
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f102414077ad0dab139c7"), "username" : "Mary", "age" : 30, "tel" : null, "friend" : [ "Jocker", "Martin" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
{ "_id" : ObjectId("5d2f105414077ad0dab139c9"), "username" : "kart", "age" : 50 }
取出 friend 數組中既有 Mary 又有 Jocker 的記錄
> db.user.find({ "friend": { $all : ["Mary", "Jocker"] } })
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin", "Kart" ] }
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
2. $size:查詢擁有指定元素個數的數組
取出 friend 數組中有3個值的記錄
> db.user.find( { "friend" : { $size : 3 } } )
{ "_id" : ObjectId("5d2f103414077ad0dab139c8"), "username" : "Martin", "age" : 40, "friend" : [ "Mary", "Jocker", "Kart" ] }
3. $slice : 返回一個數組的子集
{$slice : 10} --> 數組中的前10個
{$slice : -10} --> 數組中的後10個
{$slice : [20, 10] } --> 從數組中下標爲20的元素開始,向後去除10個元素
取出Tom的前3個朋友
> db.user.find( {"username": "Tom"}, { "friend" : {$slice : 3} } )
{ "_id" : ObjectId("5d2f0a4714077ad0dab139c5"), "username" : "Tom", "age" : 12, "tel" : "10086", "friend" : [ "Mary", "Jocker", "Martin" ] }
五、查詢內嵌文檔
1. $elemMatch
如有這樣的數據結構:
有個comments字段,該字段是一個數組,每一項是一個內嵌的comment對象
{
"_id": ObjectId("5d31b1d24fd0d7ad0a1a1361"),
"author": "Tom",
"content": "I am Tom!",
"comments": [{
"user": "Mary",
"score": 3,
"comment": "Nice!"
}, {
"user": "Martin",
"score": 6,
"comment": "I'm reading..."
}, {
"user": "Jocker",
"score": 8,
"comment": "You're kidding me"
}]
}
{
"_id": ObjectId("5d31b3114fd0d7ad0a1a1364"),
"author": "Martin",
"content": "I am Martin!",
"comments": [{
"user": "Tom",
"score": 5,
"comment": "Nice!"
}, {
"user": "Mary",
"score": 6,
"comment": "I'm reading..."
}, {
"user": "Jocker",
"score": 3,
"comment": "You're kidding me"
}]
}
{
"_id": ObjectId("5d31b3314fd0d7ad0a1a1365"),
"author": "Mary",
"content": "I am Mary!",
"comments": [{
"user": "Tom",
"score": 3,
"comment": "Nice!"
}, {
"user": "Martin",
"score": 5,
"comment": "I'm reading..."
}, {
"user": "Jocker",
"score": 2,
"comment": "You're kidding me"
}]
}
查詢評論中包含 score 大於5的文章記錄
> db.blog.find ({ "comments" : { "$elemMatch" : { "score" : {"$gt" : 5}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "author" : "Tom", "content" : "I am Tom!", "comments" : [ { "user" : "Mary", "score" : 3, "comment" : "Nice!" }, { "user" : "Martin", "score" : 6, "comment" : "I'm reading..." }, { "user" : "Jocker", "score" : 8, "comment" : "You're kidding me" } ] }
{ "_id" : ObjectId("5d31b3114fd0d7ad0a1a1364"), "author" : "Martin", "content" : "I am Martin!", "comments" : [ { "user" : "Tom", "score" : 5, "comment" : "Nice!" }, { "user" : "Mary", "score" : 6, "comment" : "I'm reading..." }, { "user" : "Jocker", "score" : 3, "comment" : "You're kidding me" } ] }
查詢Tom的文章的評論中 score 大於 5 的評論記錄
> db.blog.find ({"author": "Tom"}, { "comments" : { "$elemMatch" : { "score" : {"$gt" : 3}}} })
{ "_id" : ObjectId("5d31b1d24fd0d7ad0a1a1361"), "comments" : [ { "user" : "Martin", "score" : 6, "comment" : "I'm reading..." } ] }