MongoDB查詢文件
MongoDB 查詢文件使用find()
方法。find()
方法以非結構化的方式來顯示所有文件。
db.collection.find( <query filter>, <projection> )
- query filter 查詢條件,指明返回哪些文件
- projection 查詢對映,指定查詢的文件要返回哪些欄位
<query filter>
為空的時候就是{},可以省略。所以db.collection.find()等價於db.collection.find({})
接著上一篇的實戰,我們先來看一下users集合現在還有哪一些文件
> db.users.find() { "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" } { "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" } { "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" } { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 } { "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
下面我們通過命令來演示find()
方法的用法
1.查詢符合條件的文件
如:查詢age>25的全部文件
> db.users.find({"age": {$gt: 25}}) { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 } { "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
2.查詢符合條件的文件的指定欄位
如:查詢age>25的人的name
> db.users.find({"age": {$gt: 25}}, {name:1}) { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack" } { "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy" }
如我們所願返回了name
欄位,但是同時也返回了_id
欄位。_id
是預設返回的,如果不想要它返回許指定_id
:0
> db.users.find({"age": {$gt: 25}}, {name:1, _id:0}) { "name" : "Jack" } { "name" : "Lucy" }
3.指定AND條件
如:查詢年齡大於25的男性
> db.users.find({"age": {$gt: 25}, "gender": "male"}) { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
4.指定OR條件
如:查詢age=18的或者name是tom的文件
> db.users.find({$or: [{"name":"tom"}, {"age":18}]}) { "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" } { "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
5.指定AND和OR條件
如一下面的篩選滿足2個條件
- 年齡大於18
- 女性或者名字叫"Jack"
> db.users.find({"age":{$gt: 18}, $or:[{"gender":"female"}, {"name":"Jack"}]}) { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 } { "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
如果某一個欄位對應的是陣列,我們可以修改數組裡面的某一個欄位嗎?
先給每一個文件插入一個欄位points,它對應的值是個陣列
> var points={[ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ]} > db.users.updateMany({"age":{$gt:0}}, {$set:{"points":points}}) { "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 } > tmp=[{"points":90, "bonus":20}, {"points":88, "bonus":11}] [ { "points" : 90, "bonus" : 20 }, { "points" : 88, "bonus" : 11 } ] > db.users.update({"name": "tom"}, {$set: {"points": tmp}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) > db.users.find().pretty() > db.users.find().pretty() { "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male", "points" : [ { "points" : 90, "bonus" : 20 }, { "points" : 88, "bonus" : 11 } ] } { "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female", "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] } { "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female", "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] } { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] } { "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }
這裡我想查詢points欄位裡面第一個points為90的
> db.users.find({"points.0.points": 90}).pretty() { "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male", "points" : [ { "points" : 90, "bonus" : 20 }, { "points" : 88, "bonus" : 11 } ] }
不指定陣列索引匹配欄位
> db.users.find({"points.bonus": 11}).pretty() { "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male", "points" : [ { "points" : 90, "bonus" : 20 }, { "points" : 88, "bonus" : 11 } ] }
只有points數組裡面的bonus滿足11就返回
還可以指定陣列文件的多個查詢條件,這裡我就不掩飾了
6.null
存在性篩選
> db.student.find() { "_id" : 1, "name" : "tom", "hobby" : null } { "_id" : 2, "name" : "Jack" } > db.student.find({"hobby":null}) { "_id" : 1, "name" : "tom", "hobby" : null } { "_id" : 2, "name" : "Jack" } >
注意一下,我們篩選 hobby為null
的,沒有hobby這個欄位的夜篩選出來了
> db.student.find({"hobby":{$exists:false}}) { "_id" : 2, "name" : "Jack" } > db.student.find({"hobby":{$exists:true}}) { "_id" : 1, "name" : "tom", "hobby" : null }
通過$exists
來指定
其他查詢文件的方法findOne()
看方法名就知道值查詢返回滿足條件的第一條記錄
> db.users.findOne({"age": {$gt: 20}}) { "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1, "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }