查詢文檔
查詢文檔可以用 find() 方法查詢全部文檔,可以用 findOne() 查詢第一個文檔,當然還可以根據 條件操作符 和 $type操作符 查詢滿足條件的文檔。
1、find() 和 findOne()
MongoDB 用 find() 查詢指定集合的全部文檔
格式
db.COLLECTION_NAME.find()
范例
> db.user.find()
{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }
{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }
{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }
{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }
>
如果想要格式化顯示查詢結果,我們需要用 pretty() 方法
格式如下
db.COLLECTION_NAME.find().pretty()
再次顯示查詢結果如下
> db.user.find().pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"),
"name" : "user1",
"age" : 19,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"),
"name" : "user2",
"age" : 20,
"sex" : "woman"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"),
"name" : "user3",
"age" : 19,
"sex" : "woman"
}
>
除了 find() 方法,還有一個 findOne() 方法,它只會返回一個文檔
> db.user.findOne()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
2、條件操作符
如果你熟悉sql,那么下表可以更好的理解 MongoDB 條件查詢
| 操作 | 格式 | 范例 | RDBMS中的類似語句 |
|---|---|---|---|
| 等於 | {<key>:<value>} |
db.user.find({"name":"liruihuan"}).pretty() |
where name = 'liruihuan' |
| 小於 | {<key>:{$lt:<value>}} |
db.user.find({"age":{$lt:18}}).pretty() |
where age < 18 |
| 小於或等於 | {<key>:{$lte:<value>}} |
db.user.find({"age":{$lte:18}}).pretty() |
where age <= 18 |
| 大於 | {<key>:{$gt:<value>}} |
db.user.find({"age":{$gt:18}}).pretty() |
where age > 18 |
| 大於或等於 | {<key>:{$gte:<value>}} |
db.user.find({"age":{$gte:18}}).pretty() |
where age >= 18 |
| 不等於 | {<key>:{$ne:<value>}} |
db.user.find({"age":{$ne:18}}).pretty() |
where age != 18 |
范例
1、查詢 user 集合中 name = "liruihuan" 的文檔,代碼如下
> db.user.find({"name":"liruihuan"}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
>
2、查詢 user 集合中 age > 19 的文檔,代碼如下
> db.user.find({"age":{$gt:19}}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"),
"name" : "user2",
"age" : 20,
"sex" : "woman"
}
>
為了更好的理解條件操作符,可以用英文解釋一下
$gt -- greater than $gte -- gt equal $lt -- less than $lte -- lt equal $ne -- not equal
MongoDB 中的 and 條件
MongoDB 的 find() 方法可以傳入多個鍵(key),每個鍵(key)以逗號隔開,MongoDB 會把這些鍵作為 and 條件,及常規 SQL 的 AND 條件。
格式
db.collection.find({key1:value1, key2:value2}).pretty()
范例
查詢 user 集合中 name 值為 liruihuan 且 age 值為 18 的文檔
> db.user.find({"name":"liruihuan","age":18}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
此實例類似於 sql 中 where 條件
WHERE name='liruihuan' AND age=18
MongoDB 中的 or 條件
MongoDB 中 or 條件用 $or關鍵字
格式
db.collection.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
范例
查詢 user 集合中 name 值為 liruihuan 或 name 值為 user1 的文檔
> db.user.find({$or:[{"name":"liruihuan"},{"name":"user1"}]}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"),
"name" : "user1",
"age" : 19,
"sex" : "man"
}
>
此實例類似於 sql 中 where 條件
WHERE name='liruihuan' OR name="user1"
MongoDB 中 and 和 or 結合使用
范例
查詢 user 集合中 age 值大於17 且 (name 值為 liruihuan 或 name 值為 user1) 的文檔
> db.user.find({"age":{$gt:17},$or:[{"name":"liruihuan"},{"name":"user1"}]}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"),
"name" : "user1",
"age" : 19,
"sex" : "man"
}
>
此實例類似於 sql 中 where 條件
WHERE age > 17 (name='liruihuan' OR name="user1")
注:and 條件是在大括號中,or 條件是在中括號里的
3、$type 操作符
基於BSON類型來查詢集合中匹配的數據類型,並返回結果。
下表列舉了可使用的數據類型
| 類型 | 數字 | 備注 |
|---|---|---|
| Double | 1 | |
| String | 2 | |
| Object | 3 | |
| Array | 4 | |
| Binary data | 5 | |
| Undefined | 6 | 已廢棄。 |
| Object id | 7 | |
| Boolean | 8 | |
| Date | 9 | |
| Null | 10 | |
| Regular Expression | 11 | |
| JavaScript | 13 | |
| Symbol | 14 | |
| JavaScript (with scope) | 15 | |
| 32-bit integer | 16 | |
| Timestamp | 17 | |
| 64-bit integer | 18 | |
| Min key | 255 | Query with -1. |
| Max key | 127 |
范例
查詢 user 集合中 name 為 String 類型的文檔
> db.user.find({"name":{$type:2}}).pretty()
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"),
"name" : "liruihuan",
"age" : 18,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"),
"name" : "user1",
"age" : 19,
"sex" : "man"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"),
"name" : "user2",
"age" : 20,
"sex" : "woman"
}
{
"_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"),
"name" : "user3",
"age" : 19,
"sex" : "woman"
}
>
業精於勤,荒於嬉;行成於思,毀於隨。
如果你覺得這篇文章不錯或者對你有所幫助,可以通過右側【打賞】功能,給予博主一點點鼓勵和支持
