
- MongoDB 教程
- MongoDB - 首頁
- MongoDB - 概述
- MongoDB - 優勢
- MongoDB - 環境
- MongoDB - 資料建模
- MongoDB - 建立資料庫
- MongoDB - 刪除資料庫
- MongoDB - 建立集合
- MongoDB - 刪除集合
- MongoDB - 資料型別
- MongoDB - 插入文件
- MongoDB - 查詢文件
- MongoDB - 更新文件
- MongoDB - 刪除文件
- MongoDB - 投影
- MongoDB - 限制記錄
- MongoDB - 排序記錄
- MongoDB - 索引
- MongoDB - 聚合
- MongoDB - 複製
- MongoDB - 分片
- MongoDB - 建立備份
- MongoDB - 部署
- MongoDB - Java
- MongoDB - PHP
- 高階 MongoDB
- MongoDB - 關係
- MongoDB - 資料庫引用
- MongoDB - 覆蓋查詢
- MongoDB - 分析查詢
- MongoDB - 原子操作
- MongoDB - 高階索引
- MongoDB - 索引限制
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - 文字搜尋
- MongoDB - 正則表示式
- 使用 Rockmongo
- MongoDB - GridFS
- MongoDB - 有蓋集合
- 自動遞增序列
- MongoDB 有用資源
- MongoDB - 問答
- MongoDB - 快速指南
- MongoDB - 有用資源
- MongoDB - 討論
MongoDB - 查詢文件
在本章中,我們將學習如何從 MongoDB 集合中查詢文件。
find() 方法
要從 MongoDB 集合中查詢資料,您需要使用 MongoDB 的 find() 方法。
語法
find() 方法的基本語法如下:
>db.COLLECTION_NAME.find()
find() 方法將以非結構化的方式顯示所有文件。
示例
假設我們建立了一個名為 mycol 的集合,如下所示:
> use sampleDB switched to db sampleDB > db.createCollection("mycol") { "ok" : 1 } >
並使用 insert() 方法在其中插入了 3 個文件,如下所示:
> db.mycol.insert([ { title: "MongoDB Overview", description: "MongoDB is no SQL database", by: "tutorials point", url: "https://tutorialspoint.tw", tags: ["mongodb", "database", "NoSQL"], likes: 100 }, { title: "NoSQL Database", description: "NoSQL database doesn't have tables", by: "tutorials point", url: "https://tutorialspoint.tw", tags: ["mongodb", "database", "NoSQL"], likes: 20, comments: [ { user:"user1", message: "My first comment", dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ])
以下方法檢索集合中的所有文件:
> db.mycol.find() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] } >
pretty() 方法
要以格式化的方式顯示結果,您可以使用 pretty() 方法。
語法
>db.COLLECTION_NAME.find().pretty()
示例
以下示例從名為 mycol 的集合中檢索所有文件,並以易於閱讀的格式排列它們。
> db.mycol.find().pretty() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] }
findOne() 方法
除了 find() 方法之外,還有一個 findOne() 方法,它只返回一個文件。
語法
>db.COLLECTIONNAME.findOne()
示例
以下示例檢索標題為 MongoDB 概述的文件。
> db.mycol.findOne({title: "MongoDB Overview"}) { "_id" : ObjectId("5dd6542170fb13eec3963bf0"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
MongoDB 中的 RDBMS Where 子句等效項
要根據某些條件查詢文件,您可以使用以下操作。
操作 | 語法 | 示例 | RDBMS 等效項 |
---|---|---|---|
等於 | {<key>:{$eg;<value>}} | db.mycol.find({"by":"tutorials point"}).pretty() | where by = 'tutorials point' |
小於 | {<key>:{$lt:<value>}} | db.mycol.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
小於等於 | {<key>:{$lte:<value>}} | db.mycol.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
大於 | {<key>:{$gt:<value>}} | db.mycol.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
大於等於 | {<key>:{$gte:<value>}} | db.mycol.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
不等於 | {<key>:{$ne:<value>}} | db.mycol.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
陣列中的值 | {<key>:{$in:[<value1>, <value2>,……<valueN>]}} | db.mycol.find({"name":{$in:["Raj", "Ram", "Raghu"]}}).pretty() | Where name 匹配 :["Raj", "Ram", "Raghu"] 中的任何值 |
陣列中不存在的值 | {<key>:{$nin:<value>}} | db.mycol.find({"name":{$nin:["Ramu", "Raghav"]}}).pretty() | Where name 值不在陣列 :["Ramu", "Raghav"] 中,或者根本不存在 |
MongoDB 中的 AND
語法
要根據 AND 條件查詢文件,您需要使用 $and 關鍵字。以下是 AND 的基本語法:
>db.mycol.find({ $and: [ {<key1>:<value1>}, { <key2>:<value2>} ] })
示例
以下示例將顯示由 'tutorials point' 編寫的並且標題為 'MongoDB 概述' 的所有教程。
> db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "tutorials point", "url" : "https://tutorialspoint.tw", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } >
對於上面給出的示例,等效的 where 子句將為 ' where by = 'tutorials point' AND title = 'MongoDB Overview' '。您可以在 find 子句中傳遞任意數量的鍵值對。
MongoDB 中的 OR
語法
要根據 OR 條件查詢文件,您需要使用 $or 關鍵字。以下是 OR 的基本語法:
>db.mycol.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()
示例
以下示例將顯示由 'tutorials point' 編寫的或標題為 'MongoDB 概述' 的所有教程。
>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty() { "_id": ObjectId(7df78ad8902c), "title": "MongoDB Overview", "description": "MongoDB is no sql database", "by": "tutorials point", "url": "https://tutorialspoint.tw", "tags": ["mongodb", "database", "NoSQL"], "likes": "100" } >
同時使用 AND 和 OR
示例
以下示例將顯示喜歡次數大於 10 並且標題為 'MongoDB 概述' 或 by 為 'tutorials point' 的文件。等效的 SQL where 子句為 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"}, {"title": "MongoDB Overview"}]}).pretty() { "_id": ObjectId(7df78ad8902c), "title": "MongoDB Overview", "description": "MongoDB is no sql database", "by": "tutorials point", "url": "https://tutorialspoint.tw", "tags": ["mongodb", "database", "NoSQL"], "likes": "100" } >
MongoDB 中的 NOR
語法
要根據 NOT 條件查詢文件,您需要使用 $not 關鍵字。以下是 NOT 的基本語法:
>db.COLLECTION_NAME.find( { $not: [ {key1: value1}, {key2:value2} ] } )
示例
假設我們在 empDetails 集合中插入了 3 個文件,如下所示:
db.empDetails.insertMany( [ { First_Name: "Radhika", Last_Name: "Sharma", Age: "26", e_mail: "radhika_sharma.123@gmail.com", phone: "9000012345" }, { First_Name: "Rachel", Last_Name: "Christopher", Age: "27", e_mail: "Rachel_Christopher.123@gmail.com", phone: "9000054321" }, { First_Name: "Fathima", Last_Name: "Sheik", Age: "24", e_mail: "Fathima_Sheik.123@gmail.com", phone: "9000054321" } ] )
以下示例將檢索名字不是 "Radhika" 且姓氏不是 "Christopher" 的文件。
> db.empDetails.find( { $nor:[ 40 {"First_Name": "Radhika"}, {"Last_Name": "Christopher"} ] } ).pretty() { "_id" : ObjectId("5dd631f270fb13eec3963bef"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }
MongoDB 中的 NOT
語法
要根據 NOT 條件查詢文件,您需要使用 $not 關鍵字,以下是 NOT 的基本語法:
>db.COLLECTION_NAME.find( { $NOT: [ {key1: value1}, {key2:value2} ] } ).pretty()
示例
以下示例將檢索年齡不大於 25 的文件。
> db.empDetails.find( { "Age": { $not: { $gt: "25" } } } ) { "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }