Python 3 教程
MongoDB 中使用了 find 和 find_one 方法來查詢集合中的數(shù)據(jù),它類似于 SQL 中的 SELECT 語句。
本文使用的測試數(shù)據(jù)如下:
我們可以使用 find_one() 方法來查詢集合中的一條數(shù)據(jù)。
查詢 sites 文檔中的第一條數(shù)據(jù):
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''}
find() 方法可以查詢集合中的所有數(shù)據(jù),類似 SQL 中的 SELECT * 操作。
以下實例查找 sites 集合中的所有數(shù)據(jù):
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''} {'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'}
我們可以使用 find() 方法來查詢指定字段的數(shù)據(jù),將要返回的字段對應(yīng)值設(shè)置為 1。
輸出結(jié)果為:
{'name': 'JSON', 'alexa': '10000'} {'name': 'Google', 'alexa': '1'} {'name': 'Taobao', 'alexa': '100'} {'name': 'QQ', 'alexa': '101'} {'name': 'Facebook', 'alexa': '10'} {'name': '知乎', 'alexa': '103'} {'name': 'Github', 'alexa': '109'}
除了 _id 你不能在一個對象中同時指定 0 和 1,如果你設(shè)置了一個字段為 0,則其他都為 1,反之亦然。
以下實例除了 alexa 字段外,其他都返回:
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'url': ''} {'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'url': 'https://www.qq.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'url': 'https://www.facebook.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'url': 'https://www.github.com'}
以下代碼同時指定了 0 和 1 則會報錯:
錯誤內(nèi)容大概如下:
... pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion. ...
我們可以在 find() 中設(shè)置參數(shù)來過濾數(shù)據(jù)。
以下實例查找 name 字段為 "JSON" 的數(shù)據(jù):
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''}
查詢的條件語句中,我們還可以使用修飾符。
以下實例用于讀取 name 字段中第一個字母 ASCII 值大于 "H" 的數(shù)據(jù),大于的修飾符條件為 {"$gt": "H"} :
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''} {'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'}
我們還可以使用正則表達式作為修飾符。
正則表達式修飾符只用于搜索字符串的字段。
以下實例用于讀取 name 字段中第一個字母為 "R" 的數(shù)據(jù),正則表達式修飾符條件為 {"$regex": "^R"} :
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''}
如果我們要對查詢結(jié)果設(shè)置指定條數(shù)的記錄可以使用 limit() 方法,該方法只接受一個數(shù)字參數(shù)。
以下實例返回 3 條文檔記錄:
輸出結(jié)果為:
{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'JSON', 'alexa': '10000', 'url': ''} {'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}其他擴展