{"id":131885,"date":"2020-07-06T03:52:36","date_gmt":"2020-07-05T19:52:36","guid":{"rendered":"http:\/\/4563.org\/?p=131885"},"modified":"2020-07-06T03:52:36","modified_gmt":"2020-07-05T19:52:36","slug":"python-%e6%89%a9%e5%b1%95%e4%b9%8b-pymongo%e5%ae%89%e8%a3%85%e5%92%8c%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=131885","title":{"rendered":"python \u6269\u5c55\u4e4b PyMongo\u5b89\u88c5\u548c\u4f7f\u7528"},"content":{"rendered":"<p>PyMongo\u662f\u4e00\u4e2a\u7528\u4e8eMongoDB\u7684Python\u5de5\u5177\uff0c\u4e5f\u662f\u4e00\u4e2a\u88ab\u63a8\u8350\u7684Python\u64cd\u4f5cMongoDB\u6570\u636e\u5e93\u7684\u65b9\u5f0f\u3002<br \/>\u672c\u7ffb\u8bd1\u6587\u6863\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<p>\u4f7f\u7528PyMongo\u63d2\u5165\u6570\u636e<br \/>\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e<br \/>\u4f7f\u7528PyMongo\u66f4\u65b0\u6570\u636e<br \/>\u4f7f\u7528PyMongo\u5220\u9664\u6570\u636e<br \/>\u4f7f\u7528PyMongo\u8fdb\u884c\u6570\u636e\u805a\u5408<br \/>\u4f7f\u7528PyMongo\u521b\u5efa\u7d22\u5f15<\/p>\n<p><a href=\"https:\/\/docs.mongodb.com\/getting-started\/python\/introduction\/\">\u539f\u6587\u94fe\u63a5https:\/\/docs.mongodb.com\/getting-started\/python\/introduction\/<\/a><\/p>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u5b89\u88c5PyMongo\" title=\"\u5b89\u88c5PyMongo\"><\/a>\u5b89\u88c5PyMongo<\/h3>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>pip install pymongo<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u5f15\u5165pymongo\" title=\"\u5f15\u5165pymongo\"><\/a>\u5f15\u5165pymongo<\/h3>\n<p>\u5728\u4e00\u4e2aPython\u4ea4\u4e92shell\u4e2d\u5b8c\u6210\u6b64\u8fc7\u7a0b\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u5728\u5176\u4ed6Python\u73af\u5883\u4e2d\u5b8c\u6210\uff0c\u4f8b\u5982Python\u811a\u672c\u3001Python\u6a21\u5757\u3001Python\u9879\u76ee\u3002<\/p>\n<p>\u4ecepymongo\u4e2d\u5f15\u5165MongoClicent\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\" title=\"\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\"><\/a>\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5<\/h3>\n<p>\u4f7f\u7528MongoClient\u521b\u5efa\u4e00\u4e2a\u8fde\u63a5\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>client &#x3D; MongoClient()<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5982\u679c\u4f60\u6ca1\u6709\u7279\u522b\u4e3aMongoClient\u6307\u5b9a\u53c2\u6570\uff0cMongoClient\u5c06\u4f7f\u7528MongoDB\u9ed8\u8ba4\u53c2\u6570\u5373localhost\u63a5\u53e3\u548c27017\u7aef\u53e3\u3002<br \/>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5b8c\u6574\u7684MongoDB URI\u6765\u5b9a\u4e49\u8fde\u63a5\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>client &#x3D; MongoClient(&quot;mongodb:&#x2F;&#x2F;mongodb0.example.net:27019&quot;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2aMongoClient\u4ee3\u8868\u4e86\u4e00\u4e2a\u8fd0\u884c\u4e8emongodb.example.net\u7aef\u53e3\u53f7\u4e3a27019\u7684MongoDB\u8fde\u63a5\u3002<\/p>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u8bbf\u95ee\u6570\u636e\u5e93\u5bf9\u8c61\" title=\"\u8bbf\u95ee\u6570\u636e\u5e93\u5bf9\u8c61\"><\/a>\u8bbf\u95ee\u6570\u636e\u5e93\u5bf9\u8c61<\/h3>\n<p>\u7b2c\u4e00\u4e2a\u4f60\u7528pymongo\u6765\u4ea4\u4e92\u7684\u57fa\u672c\u7c7b\u662fDatabase\uff0c\u5b83\u4ee3\u8868\u4e86MongoDB\u4e2d\u7684\u6570\u636e\u5e93(database)\u7ed3\u6784\u3002\u6570\u636e\u5e93\u4fdd\u5b58\u4e86\u903b\u8f91\u4e0a\u76f8\u5173\u7684\u96c6\u5408\u7ec4\u3002MongoDB\u5c06\u5728\u7b2c\u4e00\u6b21\u4f7f\u7528\u65b0\u7684\u6570\u636e\u5e93\u7684\u65f6\u5019\u81ea\u52a8\u521b\u5efa\u5b83\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5c5e\u6027\u8bbf\u95ee\u7684\u65b9\u5f0f\uff0c\u8d4b\u503c\u4e00\u4e2a\u6570\u636e\u5e93\uff08\u4f8b\u5982\u540d\u5b57\u4e3aprimer\uff09\u7ed9\u672c\u5730\u53d8\u91cfdb\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>db &#x3D; client.primer<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u5f62\u5f0f\u8bbf\u95ee\u4e00\u4e2a\u6570\u636e\u5e93\uff0c\u8fd9\u4f1a\u79fb\u9664Python\u7279\u6709\u7684\u547d\u540d\u9650\u5236\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>db &#x3D; client[&#39;primer&#39;]<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u8bbf\u95ee\u96c6\u5408\u5bf9\u8c61\" title=\"\u8bbf\u95ee\u96c6\u5408\u5bf9\u8c61\"><\/a>\u8bbf\u95ee\u96c6\u5408\u5bf9\u8c61<\/h3>\n<p>\u7b2c\u4e8c\u4e2a\u4f60\u5c06\u4f7f\u7528pymongo\u8fdb\u884c\u4ea4\u4e92\u7684\u57fa\u672c\u7c7b\u662fCollection\uff0c\u5b83\u4ee3\u8868\u4e86MongoDB\u4e2d\u7684\u96c6\u5408\uff08collection\uff09\u7ed3\u6784\u3002\u96c6\u5408\uff08collection\uff09\u4fdd\u5b58\u4e86\u903b\u8f91\u4e0a\u76f8\u5173\u7684\u6587\u6863\u7ec4\u3002<br \/>\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b57\u5178\u5f62\u5f0f\u8bbf\u95ee\u96c6\u5408\u6216\u8005\u901a\u8fc7\u4e00\u4e2a\u8bbf\u95ee\u4e00\u4e2a\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u8bbf\u95ee\u96c6\u5408\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>db.dataset<\/span><br><span>db[&#39;dataset&#39;]<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u4ee5\u5728\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u8fd9\u4e2a\u96c6\u5408\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>coll &#x3D; db.dataset<\/span><br><span>coll &#x3D; db[&#39;dataset&#39;]<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528PyMongo\u63d2\u5165\u6570\u636e\" title=\"\u4f7f\u7528PyMongo\u63d2\u5165\u6570\u636e\"><\/a>\u4f7f\u7528PyMongo\u63d2\u5165\u6570\u636e<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528insert_one()\u65b9\u6cd5\u548cinsert_many()\u65b9\u6cd5\u6765\u5411MongoDB\u7684\u96c6\u5408\u4e2d\u63d2\u5165\u6587\u6863\u3002\u5982\u679c\u4f60\u6240\u63d2\u5165\u7684\u96c6\u5408\u5728MongoDB\u4e2d\u4e0d\u5b58\u5728\uff0cMongoDB\u5c06\u4e3a\u4f60\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u63d2\u5165\u4e00\u4e2a\u6587\u6863\" title=\"\u63d2\u5165\u4e00\u4e2a\u6587\u6863\"><\/a>\u63d2\u5165\u4e00\u4e2a\u6587\u6863<\/h3>\n<p>\u5411\u96c6\u5408restaurants\u4e2d\u63d2\u5165\u4e00\u4e2a\u6587\u6863\u3002\u5982\u679c\u96c6\u5408\u4e0d\u5b58\u5728\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u96c6\u5408\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><span>8<\/span><br><span>9<\/span><br><span>10<\/span><br><span>11<\/span><br><span>12<\/span><br><span>13<\/span><br><span>14<\/span><br><span>15<\/span><br><span>16<\/span><br><span>17<\/span><br><span>18<\/span><br><span>19<\/span><br><span>20<\/span><br><span>21<\/span><br><span>22<\/span><br><span>23<\/span><br><span>24<\/span><br><span>25<\/span><br><span>26<\/span><br><span>27<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from datetime import datetime<\/span><br><span>result &#x3D; db.restaurants.insert_one(<\/span><br><span>    &#123;<\/span><br><span>        &quot;address&quot;: &#123;<\/span><br><span>            &quot;street&quot;: &quot;2 Avenue&quot;,<\/span><br><span>            &quot;zipcode&quot;: &quot;10075&quot;,<\/span><br><span>            &quot;building&quot;: &quot;1480&quot;,<\/span><br><span>            &quot;coord&quot;: [-73.9557413, 40.7720266]<\/span><br><span>        &#125;,<\/span><br><span>        &quot;borough&quot;: &quot;Manhattan&quot;,<\/span><br><span>        &quot;cuisine&quot;: &quot;Italian&quot;,<\/span><br><span>        &quot;grades&quot;: [<\/span><br><span>            &#123;<\/span><br><span>                &quot;date&quot;: datetime.strptime(&quot;2014-10-01&quot;, &quot;%Y-%m-%d&quot;),<\/span><br><span>                &quot;grade&quot;: &quot;A&quot;,<\/span><br><span>                &quot;score&quot;: 11<\/span><br><span>            &#125;,<\/span><br><span>            &#123;<\/span><br><span>                &quot;date&quot;: datetime.strptime(&quot;2014-01-16&quot;, &quot;%Y-%m-%d&quot;),<\/span><br><span>                &quot;grade&quot;: &quot;B&quot;,<\/span><br><span>                &quot;score&quot;: 17<\/span><br><span>            &#125;<\/span><br><span>        ],<\/span><br><span>        &quot;name&quot;: &quot;Vella&quot;,<\/span><br><span>        &quot;restaurant_id&quot;: &quot;41704620&quot;<\/span><br><span>    &#125;<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aInsertOneResult\u5bf9\u8c61\uff0c\u5b83\u5305\u62ec\u4e86insert_id\u5c5e\u6027\u8868\u793a\u88ab\u63d2\u5165\u7684\u6587\u6863\u7684_id\u3002\u8bbf\u95eeinsert_id\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.inserted_id<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u4f60\u63d2\u5165\u7684\u6587\u6863\u7684ObjectId\u5c06\u548c\u5982\u4e0b\u6240\u793a\u7684\u4e0d\u540c\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>ObjectId(&quot;54c1478ec2341ddf130f62b7&quot;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5982\u679c\u4f60\u4f20\u9012\u7ed9insert_one()\u65b9\u6cd5\u7684\u53c2\u6570\u4e0d\u5305\u542b_id\u5b57\u6bb5\uff0cMongoClient\u5c06\u81ea\u52a8\u6dfb\u52a0\u8fd9\u4e2a\u5b57\u6bb5\u5e76\u4e14\u751f\u6210\u4e00\u4e2aObjectId\u8bbe\u7f6e\u4e3a\u8fd9\u4e2a\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e\" title=\"\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e\"><\/a>\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8-1\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7find()\u65b9\u6cd5\u4ea7\u751f\u4e00\u4e2a\u67e5\u8be2\u6765\u4eceMongoDB\u7684\u96c6\u5408\u4e2d\u67e5\u8be2\u5230\u6570\u636e\u3002MongoDB\u4e2d\u6240\u6709\u7684\u67e5\u8be2\u6761\u4ef6\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u90fd\u6709\u4e00\u4e2a\u8303\u56f4\u3002<\/p>\n<p>\u67e5\u8be2\u53ef\u4ee5\u8fd4\u56de\u5728\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6570\u636e\u6216\u8005\u53ea\u8fd4\u56de\u7b26\u5408\u7b5b\u9009\u6761\u4ef6\uff08filter\uff09\u6216\u8005\u6807\u51c6\uff08criteria\uff09\u7684\u6587\u6863\u3002\u4f60\u53ef\u4ee5\u5728\u6587\u6863\u4e2d\u6307\u5b9a\u8fc7\u6ee4\u5668\u6216\u8005\u6807\u51c6\uff0c\u5e76\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9find()\u65b9\u6cd5\u3002<\/p>\n<p>find()\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u67e5\u8be2\u7ed3\u679c\u7684\u6e38\u6807\uff0c\u8fd9\u662f\u4e00\u4e2a\u4ea7\u751f\u6587\u6863\u7684\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6-1\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u672c\u793a\u4f8b\u4e2d\u4f7f\u7528test\u6570\u636e\u5e93\u4e2d\u7684restaurants\u96c6\u5408\u3002\u4e0e\u4f7f\u7528\u793a\u4f8b\u6570\u636e\u586b\u5145\u96c6\u5408\u6709\u5173\u7684\u4ecb\u7ecd\u8bf7\u89c1Import Example Dataset\u3002<\/p>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u67e5\u8be2\u6240\u6709\u6587\u6863\" title=\"\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u67e5\u8be2\u6240\u6709\u6587\u6863\"><\/a>\u5728\u4e00\u4e2a\u96c6\u5408\u4e2d\u67e5\u8be2\u6240\u6709\u6587\u6863<\/h3>\n<p>\u8c03\u7528find()\u65b9\u5f0f\u4e0d\u9700\u8981\u4f20\u503c\u5373\u53ef\u5f97\u5230\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6587\u6863\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u4e0b\u6240\u793a\u7684\u64cd\u4f5c\u5373\u662f\u8fd4\u56derestaurants\u96c6\u5408\u4e2d\u6240\u6709\u6587\u6863\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find()<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5305\u542b\u4e86\u6240\u6709restaurants\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6307\u5b9a\u76f8\u7b49\u6761\u4ef6\" title=\"\u6307\u5b9a\u76f8\u7b49\u6761\u4ef6\"><\/a>\u6307\u5b9a\u76f8\u7b49\u6761\u4ef6<\/h4>\n<p>\u5bf9\u67d0\u4e00\u4e2a\u5b57\u6bb5\u7684\u76f8\u7b49\u6761\u4ef6\u67e5\u8be2\u6709\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>&#123; &lt;field1&gt;: &lt;value1&gt;, &lt;field2&gt;: &lt;value2&gt;, ... &#125;<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5982\u679c\u5b57\u6bb5\uff08<field>\uff09\u5728\u67d0\u4e2a\u6587\u6863\u7684\u67d0\u4e00\u4e2a\u6570\u7ec4\u5185\uff0c\u5219\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\uff08dot notation\uff09\u53bb\u8bbf\u95ee\u8be5\u5b57\u6bb5\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u8fdb\u884c\u67e5\u8be2\" title=\"\u4f7f\u7528\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u8fdb\u884c\u67e5\u8be2\"><\/a>\u4f7f\u7528\u4e00\u4e2a\u9876\u7ea7\u5b57\u6bb5\u8fdb\u884c\u67e5\u8be2<\/h4>\n<p>\u5982\u4e0b\u6240\u793a\u7684\u64cd\u4f5c\u5c06\u67e5\u8be2borough\u5b57\u6bb5\u7b49\u4e8eManhattan\u7684\u6587\u6863\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;borough&quot;: &quot;Manhattan&quot;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5728\u4e00\u4e2a\u5d4c\u5165\u5f0f\u7684\u6587\u6863\u4e2d\u67e5\u8be2\" title=\"\u5728\u4e00\u4e2a\u5d4c\u5165\u5f0f\u7684\u6587\u6863\u4e2d\u67e5\u8be2\"><\/a>\u5728\u4e00\u4e2a\u5d4c\u5165\u5f0f\u7684\u6587\u6863\u4e2d\u67e5\u8be2<\/h4>\n<p>\u8981\u6307\u5b9a\u5d4c\u5165\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u9700\u8981\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u3002\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u5c06\u5b57\u6bb5\u540d\u5305\u88f9\u3002\u4e0b\u9762\u7684\u64cd\u4f5c\u5c06\u6307\u5b9a\u4e00\u4e2a\u6587\u6863\u7684\u5730\u5740\u5b57\u5178\u4e2d\u7684\u90ae\u7f16\u5b57\u6bb5\u7684\u4e00\u4e2a\u76f8\u7b49\u7684\u6761\u4ef6\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;address.zipcode&quot;: &quot;10075&quot;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<p>\u66f4\u591a\u7684\u5173\u4e8e\u5d4c\u5165\u5f0f\u6587\u6863\u7684\u67e5\u8be2\u6761\u4ef6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605Embedded Documents\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u67e5\u8be2\" title=\"\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u67e5\u8be2\"><\/a>\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\u67e5\u8be2<\/h4>\n<p>grades\u6570\u7ec4\u5305\u542b\u4e00\u4e2a\u5d4c\u5165\u5f0f\u6587\u6863\u4f5c\u4e3a\u5176\u5143\u7d20\u3002\u5728\u8be5\u6587\u6863\u7684\u5b57\u6bb5\u4e0a\u6307\u5b9a\u4e00\u4e2a\u76f8\u7b49\u6761\u4ef6\u9700\u8981\u7528\u5230\u70b9\u64cd\u4f5c\u7b26\u3002\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u5c06\u5b57\u6bb5\u540d\u5305\u88f9\u3002\u5982\u4e0b\u6240\u793a\u7684\u67e5\u8be2\u5c06\u67e5\u8be2\u4e00\u4e2a\u6709\u5d4c\u5165\u5f0f\u6587\u6863\u7684grades\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u4e2d\u7684grade\u7b49\u4e8eB\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;grades.grade&quot;: &quot;B&quot;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<p>\u66f4\u591a\u7684\u5173\u4e8e\u6570\u7ec4\u5185\u67e5\u8be2\u6761\u4ef6\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u6570\u7ec4\u4e2d\u7279\u6b8a\u7684\u6df7\u5408\u6761\u4ef6\uff0c\u8bf7\u53c2\u9605Array\u53ca$elemMatch\u64cd\u4f5c\u7b26\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528\u64cd\u4f5c\u7b26\u6307\u5b9a\u6761\u4ef6\" title=\"\u4f7f\u7528\u64cd\u4f5c\u7b26\u6307\u5b9a\u6761\u4ef6\"><\/a>\u4f7f\u7528\u64cd\u4f5c\u7b26\u6307\u5b9a\u6761\u4ef6<\/h4>\n<p>MongoDB\u652f\u6301\u4f7f\u7528\u64cd\u4f5c\u7b26\u6307\u5b9a\u67e5\u8be2\u6761\u4ef6\uff0c\u4f8b\u5982\u6bd4\u8f83\u64cd\u4f5c\u7b26\u3002\u867d\u7136\u8fd9\u5176\u4e2d\u6709\u4e00\u4e9b\u4f8b\u5916\uff0c\u4f8b\u5982$or\u548c$and\u6761\u4ef6\u64cd\u4f5c\u7b26\u3002\u4f7f\u7528\u64cd\u4f5c\u7b26\u8fdb\u884c\u67e5\u8be2\u4e00\u822c\u6709\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>&#123; &lt;field1&gt;: &#123; &lt;operator1&gt;: &lt;value1&gt; &#125; &#125;<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5b8c\u6574\u7684\u64cd\u4f5c\u7b26\u5217\u8868\u8bf7\u67e5\u9605query opeartors(<a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/operator\/query)\u3002\">http:\/\/docs.mongodb.org\/manual\/reference\/operator\/query)\u3002<\/a><\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5927\u4e8e\uff08-gt\uff09\u64cd\u4f5c\u7b26\" title=\"\u5927\u4e8e\uff08$gt\uff09\u64cd\u4f5c\u7b26\"><\/a>\u5927\u4e8e\uff08$gt\uff09\u64cd\u4f5c\u7b26<\/h4>\n<p>\u67e5\u8be2\u5b57\u6bb5grades\u5305\u542b\u4e00\u4e2a\u5d4c\u5165\u5f0f\u6587\u6863\uff0c\u5176\u4e2dscore\u5927\u4e8e30\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;grades.score&quot;: &#123;&quot;$gt&quot;: 30&#125;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5c0f\u4e8e\uff08-lt\uff09\u64cd\u4f5c\u7b26\" title=\"\u5c0f\u4e8e\uff08$lt\uff09\u64cd\u4f5c\u7b26\"><\/a>\u5c0f\u4e8e\uff08$lt\uff09\u64cd\u4f5c\u7b26<\/h4>\n<p>\u67e5\u8be2\u5b57\u6bb5grades\u5305\u542b\u4e00\u4e2a\u5d4c\u5165\u5f0f\u6587\u6863\uff0c\u5176\u4e2dscore\u5c0f\u4e8e10\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;grades.score&quot;: &#123;&quot;$lt&quot;: 10&#125;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u7ec4\u5408\u6761\u4ef6\" title=\"\u7ec4\u5408\u6761\u4ef6\"><\/a>\u7ec4\u5408\u6761\u4ef6<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u903b\u8f91\u4e0e\uff08AND\uff09\u6216\u8005\u903b\u8f91\u6216\uff08OR\uff09\u7ec4\u5408\u591a\u4e2a\u67e5\u8be2\u6761\u4ef6\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u903b\u8f91\u4e0e\" title=\"\u903b\u8f91\u4e0e\"><\/a>\u903b\u8f91\u4e0e<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u6307\u5b9a\u4e00\u4e2a\u903b\u8f91\u4e0e\u6761\u4ef6\u67e5\u8be2\u64cd\u4f5c\uff0c\u4f7f\u7528\u9017\u53f7\u5206\u9694\u6761\u4ef6\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(&#123;&quot;cuisine&quot;: &quot;Italian&quot;, &quot;address.zipcode&quot;: &quot;10075&quot;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u903b\u8f91\u6216\" title=\"\u903b\u8f91\u6216\"><\/a>\u903b\u8f91\u6216<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528$or\u64cd\u4f5c\u7b26\u8fdb\u884c\u903b\u8f91\u6216\u6761\u4ef6\u7684\u6307\u5b9a\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.find(<\/span><br><span>    &#123;&quot;$or&quot;: [&#123;&quot;cuisine&quot;: &quot;Italian&quot;&#125;, &#123;&quot;address.zipcode&quot;: &quot;10075&quot;&#125;]&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\" title=\"\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\"><\/a>\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/h4>\n<p>\u8981\u6307\u5b9a\u7ed3\u679c\u96c6\u7684\u987a\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ffd\u52a0sort()\u65b9\u6cd5\u8fdb\u884c\u67e5\u8be2\u3002\u7ed9sort()\u65b9\u6cd5\u4f20\u9012\u9700\u8981\u6392\u5e8f\u7684\u5b57\u6bb5\u548c\u914d\u9700\u7c7b\u578b\u7b49\u3002<\/p>\n<ul>\n<li>pymongo.ASCENDING\u8868\u793a\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n<li>pymongo.DESCENDING\u8868\u793a\u964d\u5e8f\u6392\u5e8f\u3002<br \/>\u5982\u679c\u8981\u901a\u8fc7\u591a\u4e2a\u952e\u661f\u661f\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f20\u9012\u952e\u7684\u5217\u8868\u548c\u4ee5\u53ca\u5bf9\u5e94\u7684\u6392\u5e8f\u7c7b\u578b\u7684\u5217\u8868\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u4e0b\u64cd\u4f5c\u5c06\u8fd4\u56derestaurants\u96c6\u5408\u4e2d\u6240\u6709\u7684\u6587\u6863\uff0c\u5e76\u4e14\u5148\u901a\u8fc7borough\u5b57\u6bb5\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\uff0c\u7136\u540e\u5728\u6bcf\u4e2aborough\u5185\uff0c\u901a\u8fc7\u201daddress.zipcode\u201d\u5b57\u6bb5\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002<br \/>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>import pymongo<\/span><br><span>cursor &#x3D; db.restaurants.find().sort([<\/span><br><span>    (&quot;borough&quot;, pymongo.ASCENDING),<\/span><br><span>    (&quot;address.zipcode&quot;, pymongo.ASCENDING)<\/span><br><span>])<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p> \u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p> \u7ed3\u679c\u5c06\u53ea\u5305\u542b\u7b26\u5408\u6761\u4ef6\u4e14\u7ecf\u8fc7\u6392\u5e8f\u7684\u6587\u6863\u3002<\/li>\n<\/ul>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528PyMongo\u66f4\u65b0\u6570\u636e\" title=\"\u4f7f\u7528PyMongo\u66f4\u65b0\u6570\u636e\"><\/a>\u4f7f\u7528PyMongo\u66f4\u65b0\u6570\u636e<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8-2\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528update_one()\u548cupdate_many\u65b9\u6cd5\u66f4\u65b0\u96c6\u5408\u4e2d\u7684\u6587\u6863\u3002update_one()\u65b9\u6cd5\u4e00\u6b21\u66f4\u65b0\u4e00\u4e2a\u6587\u6863\u3002\u4f7f\u7528update_many()\u65b9\u6cd5\u53ef\u4ee5\u66f4\u65b0\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002\u65b9\u6cd5\u63a5\u53d7\u4ee5\u4e0b\u4e09\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u7b5b\u9009\u5668\uff0c\u53ef\u4ee5\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u8fdb\u884c\u66f4\u65b0\u3002<\/li>\n<li>\u4e00\u4e2a\u6307\u5b9a\u7684\u4fee\u6539\u8bed\u53e5<\/li>\n<li>\u81ea\u5b9a\u4e49\u66f4\u65b0\u65f6\u7684\u64cd\u4f5c\u53c2\u6570<br \/>\u8981\u6307\u5b9a\u66f4\u65b0\u65f6\u7684\u8fc7\u6ee4\u5668\uff0c\u4f7f\u7528\u548c\u67e5\u8be2\u6761\u4ef6\u65f6\u76f8\u540c\u7684\u7ed3\u6784\u5373\u53ef\u3002\u53c2\u89c1\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e\u83b7\u53d6\u67e5\u8be2\u6761\u4ef6\u7684\u4fe1\u606f\u3002<br \/>\u4f60\u4e0d\u80fd\u66f4\u65b0_id\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6-2\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u672c\u793a\u4f8b\u4e2d\u4f7f\u7528test\u6570\u636e\u5e93\u4e2d\u7684restaurants\u96c6\u5408\u3002\u4e0e\u4f7f\u7528\u793a\u4f8b\u6570\u636e\u586b\u5145\u96c6\u5408\u6709\u5173\u7684\u4ecb\u7ecd\u8bf7\u89c1Import Example Dataset\u3002<\/p>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u66f4\u65b0\u7279\u5b9a\u7684\u5b57\u6bb5\" title=\"\u66f4\u65b0\u7279\u5b9a\u7684\u5b57\u6bb5\"><\/a>\u66f4\u65b0\u7279\u5b9a\u7684\u5b57\u6bb5<\/h4>\n<p>\u8981\u6539\u53d8\u4e00\u4e2a\u7279\u5b9a\u5b57\u6bb5\u7684\u503c\uff0cMongoDB\u63d0\u4f9b\u4e86\u66f4\u65b0\u64cd\u4f5c\u7b26\uff0c\u4f8b\u5982$set\u64cd\u4f5c\u7b26\u53ef\u4ee5\u4fee\u6539\u503c\u3002\u4f8b\u5982$set\u4e4b\u7c7b\u7684\u64cd\u4f5c\u7b26\u5c06\u5728\u6ca1\u6709\u8be5\u5b57\u6bb5\u7684\u65f6\u5019\u65b0\u5efa\u8fd9\u4e2a\u5b57\u6bb5\u3002\u53ef\u4ee5\u67e5\u9605update operators\u4f5c\u4e3a\u53c2\u8003\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u66f4\u65b0\u9876\u7ea7\u5b57\u6bb5\" title=\"\u66f4\u65b0\u9876\u7ea7\u5b57\u6bb5\"><\/a>\u66f4\u65b0\u9876\u7ea7\u5b57\u6bb5<\/h4>\n<p>\u5982\u4e0b\u64cd\u4f5c\u5c06\u66f4\u65b0\u7b2c\u4e00\u4e2a\u7b26\u5408name\u7b49\u4e8eJuni\u8fd9\u4e2a\u6761\u4ef6\u7684\u6587\u6863\u3002\u4f7f\u7528$set\u64cd\u4f5c\u7b26\u66f4\u65b0cuisine\u5b57\u6bb5\u4e14\u5c06lastModified\u4fee\u6539\u4e3a\u5f53\u524d\u65e5\u671f\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><span>8<\/span><br><span>9<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.update_one(<\/span><br><span>    &#123;&quot;name&quot;: &quot;Juni&quot;&#125;,<\/span><br><span>    &#123;<\/span><br><span>        &quot;$set&quot;: &#123;<\/span><br><span>            &quot;cuisine&quot;: &quot;American (New)&quot;<\/span><br><span>        &#125;,<\/span><br><span>        &quot;$currentDate&quot;: &#123;&quot;lastModified&quot;: True&#125;<\/span><br><span>    &#125;<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aUpdateResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\u4ee5\u53ca\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u7b26\u5408\u7b5b\u9009\u5668\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684matched_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.matched_count<\/span><br><span>matched_count\u503c\u4e3a\uff1a1<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8981\u67e5\u770b\u66f4\u65b0\u64cd\u4f5c\u4e2d\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684modified_count\u5c5e\u6027\u3002<\/p>\n<p>modified_count\u503c\u4e3a\uff1a 1<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u66f4\u65b0\u5d4c\u5165\u5f0f\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\" title=\"\u66f4\u65b0\u5d4c\u5165\u5f0f\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\"><\/a>\u66f4\u65b0\u5d4c\u5165\u5f0f\u6587\u6863\u4e2d\u7684\u5b57\u6bb5<\/h4>\n<p>\u8981\u66f4\u65b0\u4e00\u4e2a\u5d4c\u5165\u5f0f\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\uff0c\u9700\u8981\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u3002\u5f53\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u65f6\uff0c\u4f7f\u7528\u70b9\u64cd\u4f5c\u7b26\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u5c06\u5b57\u6bb5\u540d\u5305\u88f9\u3002\u4e0b\u9762\u7684\u64cd\u4f5c\u5c06\u66f4\u65b0address\u5b57\u6bb5\u4e2d\u7684street\u503c\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.update_one(<\/span><br><span>    &#123;&quot;restaurant_id&quot;: &quot;41156888&quot;&#125;,<\/span><br><span>    &#123;&quot;$set&quot;: &#123;&quot;address.street&quot;: &quot;East 31st Street&quot;&#125;&#125;<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aUpdateResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\u4ee5\u53ca\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u7b26\u5408\u7b5b\u9009\u5668\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684matched_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.matched_count<\/span><br><span>matched_count\u503c\u4e3a\uff1a1<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8981\u67e5\u770b\u66f4\u65b0\u64cd\u4f5c\u4e2d\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684modified_count\u5c5e\u6027\u3002<\/p>\n<p>modified_count\u503c\u4e3a\uff1a1<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u66f4\u65b0\u591a\u4e2a\u6587\u6863\" title=\"\u66f4\u65b0\u591a\u4e2a\u6587\u6863\"><\/a>\u66f4\u65b0\u591a\u4e2a\u6587\u6863<\/h4>\n<p>update_one()\u65b9\u6cd5\u66f4\u65b0\u4e86\u4e00\u4e2a\u6587\u6863\uff0c\u8981\u66f4\u65b0\u591a\u4e2a\u6587\u6863\uff0c\u9700\u8981\u4f7f\u7528update_many()\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u64cd\u4f5c\u5c06\u66f4\u65b0\u6240\u6709\u7684address.zipcode\u7b49\u4e8e10016\u4ee5\u53cacuisine\u7b49\u4e8eOther\u7684\u6587\u6863\uff0c\u5c06cuisine\u5b57\u6bb5\u8bbe\u7f6e\u4e3aCategory To Be Determined\u4ee5\u53ca\u5c06lastModified\u66f4\u65b0\u4e3a\u5f53\u524d\u65e5\u671f\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.update_many(<\/span><br><span>    &#123;&quot;address.zipcode&quot;: &quot;10016&quot;, &quot;cuisine&quot;: &quot;Other&quot;&#125;,<\/span><br><span>    &#123;<\/span><br><span>        &quot;$set&quot;: &#123;&quot;cuisine&quot;: &quot;Category To Be Determined&quot;&#125;,<\/span><br><span>        &quot;$currentDate&quot;: &#123;&quot;lastModified&quot;: True&#125;<\/span><br><span>    &#125;<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aUpdateResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\u4ee5\u53ca\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u7b26\u5408\u7b5b\u9009\u5668\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684matched_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.matched_count<\/span><br><span>matched_count\u503c\u4e3a\uff1a20<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8981\u67e5\u770b\u66f4\u65b0\u64cd\u4f5c\u4e2d\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684modified_count\u5c5e\u6027\u3002<\/p>\n<p>modified_count\u503c\u4e3a\uff1a20<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u66ff\u6362\u4e00\u4e2a\u6587\u6863\" title=\"\u66ff\u6362\u4e00\u4e2a\u6587\u6863\"><\/a>\u66ff\u6362\u4e00\u4e2a\u6587\u6863<\/h4>\n<p>\u8981\u66ff\u6362\u6574\u4e2a\u6587\u6863\uff08\u9664\u4e86_id\u5b57\u6bb5\uff09\uff0c\u5c06\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u6863\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u7ed9update()\u65b9\u6cd5\u3002\u66ff\u4ee3\u6587\u6863\u5bf9\u5e94\u539f\u6765\u7684\u6587\u6863\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u5b57\u6bb5\u3002\u5728\u66ff\u4ee3\u6587\u6863\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5ffd\u7565_id\u5b57\u6bb5\u56e0\u4e3a\u5b83\u662f\u4e0d\u53d8\u7684\u3002\u5982\u679c\u4f60\u5305\u542b\u4e86_id\u5b57\u6bb5\uff0c\u90a3\u5b83\u5fc5\u987b\u548c\u539f\u6587\u6863\u7684\u503c\u76f8\u540c\u3002<\/p>\n<p>\u91cd\u8981\uff1a<br \/>\u5728\u66f4\u65b0\u4e4b\u540e\uff0c\u8be5\u6587\u6863\u5c06\u53ea\u5305\u542b\u66ff\u4ee3\u6587\u6863\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u5728\u5982\u4e0b\u7684\u66f4\u65b0\u64cd\u4f5c\u540e\uff0c\u88ab\u4fee\u6539\u7684\u6587\u6863\u5c06\u53ea\u5269\u4e0b_id\u3001name\u548caddress\u5b57\u6bb5\u3002\u8be5\u6587\u6863\u5c06\u4e0d\u518d\u5305\u542brestaurant_id\u3001cuisine\u3001grades\u4ee5\u53caborough\u5b57\u6bb5\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><span>8<\/span><br><span>9<\/span><br><span>10<\/span><br><span>11<\/span><br><span>12<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.replace_one(<\/span><br><span>    &#123;&quot;restaurant_id&quot;: &quot;41704620&quot;&#125;,<\/span><br><span>    &#123;<\/span><br><span>        &quot;name&quot;: &quot;Vella 2&quot;,<\/span><br><span>        &quot;address&quot;: &#123;<\/span><br><span>            &quot;coord&quot;: [-73.9557413, 40.7720266],<\/span><br><span>            &quot;building&quot;: &quot;1480&quot;,<\/span><br><span>            &quot;street&quot;: &quot;2 Avenue&quot;,<\/span><br><span>            &quot;zipcode&quot;: &quot;10075&quot;<\/span><br><span>        &#125;<\/span><br><span>    &#125;<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>replace_one\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aUpdateResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\u4ee5\u53ca\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u7b26\u5408\u7b5b\u9009\u5668\u6761\u4ef6\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684matched_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.matched_count<\/span><br><span>matched_count\u503c\u4e3a\uff1a20<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8981\u67e5\u770b\u66f4\u65b0\u64cd\u4f5c\u4e2d\u88ab\u4fee\u6539\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeUpdateResult\u5bf9\u8c61\u7684modified_count\u5c5e\u6027\u3002<\/p>\n<p>modified_count\u503c\u4e3a\uff1a20<\/p>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528PyMongo\u5220\u9664\u6570\u636e\" title=\"\u4f7f\u7528PyMongo\u5220\u9664\u6570\u636e\"><\/a>\u4f7f\u7528PyMongo\u5220\u9664\u6570\u636e<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8-3\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528delete_one()\u4ee5\u53cadelete_many()\u65b9\u6cd5\u4ece\u96c6\u5408\u4e2d\u5220\u9664\u6587\u6863\u3002\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u6761\u4ef6\u6765\u786e\u5b9a\u9700\u8981\u5220\u9664\u7684\u6587\u6863\u3002<\/p>\n<p>\u8981\u6307\u5b9a\u4e00\u4e2a\u5220\u9664\u6761\u4ef6\uff0c\u4f7f\u7528\u548c\u67e5\u8be2\u6761\u4ef6\u65f6\u76f8\u540c\u7684\u7ed3\u6784\u5373\u53ef\u3002\u53c2\u89c1\u4f7f\u7528PyMongo\u67e5\u8be2\u6570\u636e\u83b7\u53d6\u67e5\u8be2\u6761\u4ef6\u7684\u4fe1\u606f\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6-3\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u672c\u793a\u4f8b\u4e2d\u4f7f\u7528test\u6570\u636e\u5e93\u4e2d\u7684restaurants\u96c6\u5408\u3002\u4e0e\u4f7f\u7528\u793a\u4f8b\u6570\u636e\u586b\u5145\u96c6\u5408\u6709\u5173\u7684\u4ecb\u7ecd\u8bf7\u89c1Import Example Dataset\u3002<\/p>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6b65\u9aa4\" title=\"\u6b65\u9aa4\"><\/a>\u6b65\u9aa4<\/h4>\n<p>\u5220\u9664\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6587\u6863<br \/>\u4e0b\u9762\u7684\u64cd\u4f5c\u5c06\u5220\u9664\u6240\u6709\u590d\u5408\u6761\u4ef6\u7684\u6587\u6863\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.delete_many(&#123;&quot;borough&quot;: &quot;Manhattan&quot;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aDeleteResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u88ab\u5220\u9664\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u88ab\u5220\u9664\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeDeleteResult\u5bf9\u8c61\u7684deleted_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.deleted_count<\/span><br><span>deleted_count\u503c\u4e3a\uff1a10259<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5982\u679c\u4f60\u5df2\u7ecf\u63d2\u5165\u6216\u8005\u66f4\u65b0\u4e86\u6587\u6863\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u7ed3\u679c\u5c06\u548c\u793a\u4f8b\u4e0d\u540c\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5220\u9664\u6240\u6709\u6587\u6863\" title=\"\u5220\u9664\u6240\u6709\u6587\u6863\"><\/a>\u5220\u9664\u6240\u6709\u6587\u6863<\/h4>\n<p>\u8981\u5220\u9664\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6587\u6863\uff0c\u7ed9delete_many()\u65b9\u6cd5\u4f20\u9012\u4e00\u4e2a\u7a7a\u7684\u6761\u4ef6\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result &#x3D; db.restaurants.delete_many(&#123;&#125;)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u8fd4\u56de\u4e86\u4e00\u4e2aDeleteResult\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u62a5\u544a\u4e86\u88ab\u5220\u9664\u7684\u6587\u6863\u6570\u76ee\u3002<\/p>\n<p>\u8981\u67e5\u770b\u88ab\u5220\u9664\u7684\u6587\u6863\u6570\u76ee\uff0c\u901a\u8fc7\u8bbf\u95eeDeleteResult\u5bf9\u8c61\u7684deleted_count\u5c5e\u6027\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>result.deleted_count<\/span><br><span>deleted_count\u503c\u4e3a\uff1a15100<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u5982\u679c\u4f60\u5df2\u7ecf\u63d2\u5165\u6216\u8005\u66f4\u65b0\u4e86\u6587\u6863\uff0c\u90a3\u4e48\u4f60\u5f97\u5230\u7684\u7ed3\u679c\u5c06\u548c\u793a\u4f8b\u4e0d\u540c\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u9500\u6bc1\u4e00\u4e2a\u96c6\u5408\" title=\"\u9500\u6bc1\u4e00\u4e2a\u96c6\u5408\"><\/a>\u9500\u6bc1\u4e00\u4e2a\u96c6\u5408<\/h4>\n<p>\u5220\u9664\u6240\u6709\u6587\u6863\u7684\u64cd\u4f5c\u53ea\u4f1a\u6e05\u7a7a\u96c6\u5408\u4e2d\u7684\u6587\u6863\u3002\u8be5\u96c6\u5408\u4ee5\u53ca\u96c6\u5408\u7684\u7d22\u5f15\u5c06\u4f9d\u65e7\u5b58\u5728\u3002\u8981\u6e05\u7a7a\u4e00\u4e2a\u96c6\u5408\uff0c\u9500\u6bc1\u8be5\u96c6\u5408\u4ee5\u53ca\u5b83\u7684\u7d22\u5f15\u5e76\u4e14\u91cd\u5efa\u96c6\u5408\u548c\u7d22\u5f15\u53ef\u80fd\u662f\u76f8\u6bd4\u4e8e\u6e05\u7a7a\u4e00\u4e2a\u96c6\u5408\u66f4\u52a0\u9ad8\u6548\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\u4f7f\u7528drop()\u65b9\u6cd5\u53ef\u4ee5\u9500\u6bc1\u4e00\u4e2a\u96c6\u5408\uff0c\u5305\u62ec\u5b83\u6240\u6709\u7684\u7d22\u5f15\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>db.restaurants.drop()<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#\u4f7f\u7528PyMongo\u8fdb\u884c\u6570\u636e\u805a\u5408\" title=\"\u4f7f\u7528PyMongo\u8fdb\u884c\u6570\u636e\u805a\u5408\"><\/a>\u4f7f\u7528PyMongo\u8fdb\u884c\u6570\u636e\u805a\u5408<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8-4\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>MongoDB\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u805a\u5408\u64cd\u4f5c\uff0c\u4f8b\u5982\u53ef\u4ee5\u9488\u5bf9\u67d0\u4e00\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u6216\u8005\u5bf9\u67d0\u4e00\u5b57\u6bb5\u4e0d\u540c\u7684\u503c\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<p>\u4f7f\u7528aggregate()\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528\u57fa\u4e8e\u6b65\u9aa4\u7684\u805a\u5408\u64cd\u4f5c\u3002appregate()\u65b9\u6cd5\u63a5\u53d7\u591a\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u6bcf\u4e00\u6b65\u7684\u64cd\u4f5c\u3002\u6bcf\u4e00\u4e2a\u9636\u6bb5\u6309\u7167\u987a\u5e8f\u5904\u7406\uff0c\u63cf\u8ff0\u4e86\u5bf9\u6570\u636e\u64cd\u4f5c\u7684\u6b65\u9aa4\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>db.collection.aggregate([&lt;stage1&gt;, &lt;stage2&gt;, ...])<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6-4\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u8fd9\u90e8\u5206\u7684\u4f8b\u5b50\u4f7f\u7528\u4e86test\u6570\u636e\u5e93\u4e2d\u7684restaurants\u96c6\u5408\u3002\u9700\u8981\u67e5\u770b\u5728\u96c6\u5408\u4e2d\u586b\u5145\u7684\u5b9e\u4f8b\u6570\u636e\uff0c\u8bf7\u53c2\u9605Import Example Dataset\u3002<\/p>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6839\u636e\u4e00\u4e2a\u5b57\u6bb5\u5206\u7ec4\u6587\u4ef6\u5e76\u8ba1\u7b97\u603b\u6570\" title=\"\u6839\u636e\u4e00\u4e2a\u5b57\u6bb5\u5206\u7ec4\u6587\u4ef6\u5e76\u8ba1\u7b97\u603b\u6570\"><\/a>\u6839\u636e\u4e00\u4e2a\u5b57\u6bb5\u5206\u7ec4\u6587\u4ef6\u5e76\u8ba1\u7b97\u603b\u6570<\/h4>\n<p>\u4f7f\u7528$group\u64cd\u4f5c\u7b26\u53bb\u5229\u7528\u4e00\u4e2a\u6307\u5b9a\u7684\u952e\u8fdb\u884c\u5206\u7ec4\u3002\u5728$group\u64cd\u4f5c\u4e2d\uff0c\u6307\u5b9a\u9700\u8981\u5206\u7ec4\u7684\u5b57\u6bb5\u4e3a_id\u3002$group\u901a\u8fc7\u5b57\u6bb5\u8def\u5f84\u8bbf\u95ee\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u9700\u8981\u6709\u4e00\u4e2a\u7f8e\u5143\u7b26\u53f7$\u4f5c\u4e3a\u524d\u7f00\u3002$group\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528\u7d2f\u52a0\u5668\u5bf9\u672c\u6b21\u5206\u7ec4\u8fdb\u884c\u8ba1\u7b97\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4f7f\u7528borough\u5b57\u6bb5\u5bf9restaurants\u96c6\u5408\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u4e14\u4f7f\u7528$sum\u7d2f\u52a0\u5668\u8fdb\u884c\u6587\u6863\u7684\u7edf\u8ba1\u8ba1\u7b97\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.aggregate(<\/span><br><span>    [<\/span><br><span>        &#123;&quot;$group&quot;: &#123;&quot;_id&quot;: &quot;$borough&quot;, &quot;count&quot;: &#123;&quot;$sum&quot;: 1&#125;&#125;&#125;<\/span><br><span>    ]<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><span>8<\/span><br><span>9<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><span>\u7ed3\u679c\u5c06\u7531\u5982\u4e0b\u6587\u6863\u7ec4\u6210\uff1a<\/span><br><span>&#123;u&#39;count&#39;: 969, u&#39;_id&#39;: u&#39;Staten Island&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 6086, u&#39;_id&#39;: u&#39;Brooklyn&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 10259, u&#39;_id&#39;: u&#39;Manhattan&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 5656, u&#39;_id&#39;: u&#39;Queens&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 2338, u&#39;_id&#39;: u&#39;Bronx&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 51, u&#39;_id&#39;: u&#39;Missing&#39;&#125;<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>_id\u5b57\u6bb5\u5305\u542b\u4e86\u4e0d\u540c\u7684borough\u503c\uff0c\u5373\u6839\u636e\u952e\u7684\u503c\u8fdb\u884c\u4e86\u5206\u7ec4\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u7b5b\u9009\u5e76\u5206\u7ec4\u6587\u6863\" title=\"\u7b5b\u9009\u5e76\u5206\u7ec4\u6587\u6863\"><\/a>\u7b5b\u9009\u5e76\u5206\u7ec4\u6587\u6863<\/h4>\n<p>\u4f7f\u7528$match\u64cd\u4f5c\u6765\u5220\u9009\u6587\u6863\u3002$match\u4f7f\u7528MongoDB\u67e5\u8be2\u8bed\u6cd5\u3002\u4e0b\u9762\u7684\u7ba1\u9053\u4f7f\u7528$match\u6765\u5bf9restaurants\u8fdb\u884c\u4e00\u4e2aborough\u7b49\u4e8e\u201dQueens\u201d\u4e14cuisine\u7b49\u4e8eBrazilian\u7684\u67e5\u8be2\u3002\u63a5\u7740$group\u64cd\u4f5c\u5bf9\u547d\u4e2d\u7684\u6587\u6863\u4f7f\u7528address.zipcode\u5b57\u6bb5\u5206\u7ec4\u5e76\u4f7f\u7528$sum\u8fdb\u884c\u7edf\u8ba1\u3002$group\u901a\u8fc7\u5b57\u6bb5\u8def\u5f84\u8bbf\u95ee\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u9700\u8981\u6709\u4e00\u4e2a\u7f8e\u5143\u7b26\u53f7$\u4f5c\u4e3a\u524d\u7f00\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>cursor &#x3D; db.restaurants.aggregate(<\/span><br><span>    [<\/span><br><span>        &#123;&quot;$match&quot;: &#123;&quot;borough&quot;: &quot;Queens&quot;, &quot;cuisine&quot;: &quot;Brazilian&quot;&#125;&#125;,<\/span><br><span>        &#123;&quot;$group&quot;: &#123;&quot;_id&quot;: &quot;$address.zipcode&quot;, &quot;count&quot;: &#123;&quot;$sum&quot;: 1&#125;&#125;&#125;<\/span><br><span>    ]<\/span><br><span>)<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8fed\u4ee3\u6e38\u6807\uff08cursor\uff09\u5e76\u4e14\u6253\u5370\u6587\u6863\u5185\u5bb9\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><span>6<\/span><br><span>7<\/span><br><span>8<\/span><br><span>9<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>for document in cursor:<\/span><br><span>    print(document)<\/span><br><span>\u7ed3\u679c\u5c06\u7531\u5982\u4e0b\u6587\u6863\u7ec4\u6210\uff1a<\/span><br><span><\/span><br><span>&#123;u&#39;count&#39;: 1, u&#39;_id&#39;: u&#39;11368&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 3, u&#39;_id&#39;: u&#39;11106&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 1, u&#39;_id&#39;: u&#39;11377&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 1, u&#39;_id&#39;: u&#39;11103&#39;&#125;<\/span><br><span>&#123;u&#39;count&#39;: 2, u&#39;_id&#39;: u&#39;11101&#39;&#125;<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>_id\u5b57\u6bb5\u5305\u542b\u4e86\u4e0d\u540c\u7684zipcode\u503c\uff0c\u5373\u6839\u636e\u952e\u7684\u503c\u8fdb\u884c\u4e86\u5206\u7ec4\u3002<\/p>\n<h3><a href=\"https:\/\/www.studytime.xin\/article\/#PyMongo\u4e0a\u7684\u7d22\u5f15\" title=\"PyMongo\u4e0a\u7684\u7d22\u5f15\"><\/a>PyMongo\u4e0a\u7684\u7d22\u5f15<\/h3>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u6982\u89c8-5\" title=\"\u6982\u89c8\"><\/a>\u6982\u89c8<\/h4>\n<p>\u7d22\u5f15\u53ef\u4ee5\u5bf9\u67e5\u8be2\u7684\u9ad8\u6548\u6267\u884c\u8d77\u5230\u652f\u6301\u3002\u5982\u679c\u6ca1\u6709\u7d22\u5f15\uff0cMongoDB\u5fc5\u987b\u8fdb\u884c\u5168\u8868\u626b\u63cf\uff0c\u5373\u626b\u63cf\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u6587\u6863\uff0c\u6765\u9009\u62e9\u7b26\u5408\u67e5\u8be2\u6761\u4ef6\u7684\u6587\u6863\u3002\u5982\u679c\u4e00\u4e2a\u9002\u5f53\u7684\u7d22\u5f15\u5b58\u5728\u4e8e\u4e00\u4e2a\u67e5\u8be2\u4e2d\uff0cMongoDB\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u9650\u5236\u5fc5\u987b\u68c0\u67e5\u6587\u6863\u7684\u6570\u91cf\u3002<\/p>\n<p>\u4f7f\u7528create_index()\u65b9\u6cd5\u6765\u4e3a\u4e00\u4e2a\u96c6\u5408\u521b\u5efa\u7d22\u5f15\u3002\u6240\u4ee5\u53ef\u4ee5\u5bf9\u67e5\u8be2\u7684\u9ad8\u6548\u6267\u884c\u8d77\u5230\u652f\u6301\u3002MongoDB\u4f1a\u5728\u521b\u5efa\u6587\u6863\u7684\u65f6\u5019\u81ea\u52a8\u4e3a_id\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\u3002<\/p>\n<p>\u8981\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\uff0c\u4f7f\u7528\u4e00\u4e2a\u5305\u542b\u5b57\u6bb5\u548c\u7d22\u5f15\u7c7b\u578b\u7684\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff1a<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>[ ( &lt;field1&gt;: &lt;type1&gt; ), ... ]<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u5347\u5e8f\u7684\u7d22\u5f15\uff0c\u6307\u5b9apymongo.ASCENDING\u4e3a\u7d22\u5f15\u7c7b\u578b\uff08\uff09\u3002<br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u964d\u5e8f\u7684\u7d22\u5f15\uff0c\u6307\u5b9apymongo.DESCENDING\u4e3a\u7d22\u5f15\u7c7b\u578b\uff08\uff09\u3002<br \/>create_index()\u53ea\u4f1a\u5728\u7d22\u5f15\u4e0d\u5b58\u5728\u7684\u65f6\u5019\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u3002<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u5148\u51b3\u6761\u4ef6-5\" title=\"\u5148\u51b3\u6761\u4ef6\"><\/a>\u5148\u51b3\u6761\u4ef6<\/h4>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4f7f\u7528test\u6570\u636e\u5e93\u4e2d\u7684restaurants\u96c6\u5408\u3002\u9700\u8981\u67e5\u770b\u5728\u96c6\u5408\u4e2d\u586b\u5145\u7684\u5b9e\u4f8b\u6570\u636e\uff0c\u8bf7\u53c2\u9605Import Example Dataset\u3002<\/p>\n<p>\u5728Python\u547d\u4ee4\u884c\u6216\u8005IDLE\u4e2d\uff0c\u4f7f\u7528MongoClient\u8fde\u63a5\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684MongoDB\u5b9e\u4f8b\uff0c\u4e14\u5df2\u7ecf\u6253\u5f00test\u6570\u636e\u5e93\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>from pymongo import MongoClient<\/span><br><span><\/span><br><span>client &#x3D; MongoClient()<\/span><br><span>db &#x3D; client.test<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u521b\u5efa\u4e00\u4e2a\u5355\u5b57\u6bb5\u7d22\u5f15\" title=\"\u521b\u5efa\u4e00\u4e2a\u5355\u5b57\u6bb5\u7d22\u5f15\"><\/a>\u521b\u5efa\u4e00\u4e2a\u5355\u5b57\u6bb5\u7d22\u5f15<\/h4>\n<p>\u5728restaurants\u96c6\u5408\u4e2d\u7684cuisine\u5b57\u6bb5\u4e0a\u521b\u5efa\u81ea\u589e\u7684\u7d22\u5f15\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>import pymongo<\/span><br><span>db.restaurants.create_index([(&quot;cuisine&quot;, pymongo.ASCENDING)])<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u88ab\u521b\u5efa\u7684\u7d22\u5f15\u7684\u540d\u5b57\u3002<\/p>\n<p>\u201cu\u2019cuisine_1\u2019\u201d<\/p>\n<h4><a href=\"https:\/\/www.studytime.xin\/article\/#\u521b\u5efa\u4e00\u4e2a\u590d\u5408\u7d22\u5f15\" title=\"\u521b\u5efa\u4e00\u4e2a\u590d\u5408\u7d22\u5f15\"><\/a>\u521b\u5efa\u4e00\u4e2a\u590d\u5408\u7d22\u5f15<\/h4>\n<p>MongoDB\u652f\u6301\u5728\u591a\u4e2a\u5b57\u6bb5\u4e0a\u521b\u5efa\u7b26\u5408\u7d22\u5f15\u3002\u8fd9\u51e0\u4e2a\u5b57\u6bb5\u7684\u547d\u4ee4\u5c06\u58f0\u660e\u8fd9\u4e2a\u7d22\u5f15\u5305\u542b\u7684\u952e\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u7684\u64cd\u4f5c\u5c06\u5728cuisine\u5b57\u6bb5\u548caddress.zipcode\u5b57\u6bb5\u4e0a\u521b\u5efa\u4e00\u4e2a\u590d\u5408\u7d22\u5f15\u3002\u8be5\u7d22\u5f15\u5c06\u5148\u5bf9cuisine\u7684\u503c\u8f93\u5165\u4e00\u4e2a\u5347\u5e8f\u7684\u547d\u4ee4\uff0c\u7136\u540e\u5bf9address.zipcode\u7684\u503c\u8f93\u5165\u4e00\u4e2a\u964d\u5e8f\u547d\u4ee4\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><span>2<\/span><br><span>3<\/span><br><span>4<\/span><br><span>5<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>import pymongo<\/span><br><span>db.restaurants.create_index([<\/span><br><span>    (&quot;cuisine&quot;, pymongo.ASCENDING),<\/span><br><span>    (&quot;address.zipcode&quot;, pymongo.DESCENDING)<\/span><br><span>])<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<p>\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u88ab\u521b\u5efa\u7684\u7d22\u5f15\u7684\u540d\u5b57\u3002<\/p>\n<figure>\n<table>\n<tr>\n<td>\n<pre><span>1<\/span><br><\/pre>\n<\/td>\n<td>\n<pre><span>&quot;u&#39;cuisine_1_address.zipcode_-1&#39;&quot;<\/span><br><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<\/figure>\n<blockquote>\n<p>\u672c\u6587\u8f6c\u8f7d\u81ea\u7166\u7684\u535a\u5ba2\u7684\u4e2a\u4eba\u535a\u5ba2,\u539f\u6587\u94fe\u63a5\u4e3a<a href=\"https:\/\/www.cnblogs.com\/zhouxuchen\/p\/5544227.html\">https:\/\/www.cnblogs.com\/zhouxuchen\/p\/5544227.html<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>PyMongo\u662f\u4e00\u4e2a\u7528\u4e8eMongo&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/131885"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131885"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/131885\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131885"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}