{"id":63836,"date":"2024-10-14T02:53:16","date_gmt":"2024-10-13T18:53:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63836\/"},"modified":"2024-10-14T02:53:16","modified_gmt":"2024-10-13T18:53:16","slug":"mongodb%e8%a1%93%e8%aa%9e-%e6%b8%b8%e6%a8%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63836\/","title":{"rendered":"MongoDB\u8853\u8a9e &#8211; \u6e38\u6a19"},"content":{"rendered":"<h1 id=\"mongodb%e8%a1%93%e8%aa%9e-%e6%b8%b8%e6%a8%99-XFbmBneemt\">MongoDB\u8853\u8a9e &#8211; \u6e38\u6a19<\/h1>\n<p>\u5728MongoDB\u4e2d\uff0c\u6e38\u6a19\uff08Cursor\uff09\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5c0d\u65bc\u6578\u64da\u7684\u67e5\u8a62\u548c\u64cd\u4f5c\u81f3\u95dc\u91cd\u8981\u3002\u6e38\u6a19\u662f\u4e00\u7a2e\u6307\u5411\u67e5\u8a62\u7d50\u679c\u7684\u6307\u6a19\uff0c\u5141\u8a31\u7528\u6236\u9010\u884c\u8a2a\u554f\u67e5\u8a62\u8fd4\u56de\u7684\u6578\u64da\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u6e38\u6a19\u7684\u5b9a\u7fa9\u3001\u7279\u6027\u3001\u4f7f\u7528\u65b9\u6cd5\u4ee5\u53ca\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e6%b8%b8%e6%a8%99%e7%9a%84%e5%ae%9a%e7%be%a9-XFbmBneemt\">\u6e38\u6a19\u7684\u5b9a\u7fa9<\/h2>\n<p>\u6e38\u6a19\u662fMongoDB\u67e5\u8a62\u64cd\u4f5c\u7684\u7d50\u679c\u96c6\u7684\u6307\u6a19\u3002\u7576\u7528\u6236\u57f7\u884c\u67e5\u8a62\u6642\uff0cMongoDB\u6703\u8fd4\u56de\u4e00\u500b\u6e38\u6a19\u5c0d\u8c61\uff0c\u8a72\u5c0d\u8c61\u5305\u542b\u67e5\u8a62\u7d50\u679c\u7684\u6240\u6709\u6587\u6a94\u3002\u7528\u6236\u53ef\u4ee5\u901a\u904e\u6e38\u6a19\u9010\u4e00\u8a2a\u554f\u9019\u4e9b\u6587\u6a94\uff0c\u800c\u4e0d\u5fc5\u4e00\u6b21\u6027\u5c07\u6240\u6709\u6578\u64da\u52a0\u8f09\u5230\u5167\u5b58\u4e2d\u3002<\/p>\n<h2 id=\"%e6%b8%b8%e6%a8%99%e7%9a%84%e7%89%b9%e6%80%a7-XFbmBneemt\">\u6e38\u6a19\u7684\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u5ef6\u9072\u52a0\u8f09\uff1a<\/strong>\u6e38\u6a19\u4e0d\u6703\u7acb\u5373\u52a0\u8f09\u6240\u6709\u6578\u64da\uff0c\u800c\u662f\u6839\u64da\u9700\u8981\u9010\u6b65\u52a0\u8f09\uff0c\u9019\u6a23\u53ef\u4ee5\u7bc0\u7701\u5167\u5b58\u3002<\/li>\n<li><strong>\u53ef\u8fed\u4ee3\u6027\uff1a<\/strong>\u6e38\u6a19\u652f\u6301\u8fed\u4ee3\u64cd\u4f5c\uff0c\u7528\u6236\u53ef\u4ee5\u4f7f\u7528\u8ff4\u5708\u9010\u4e00\u8a2a\u554f\u6bcf\u500b\u6587\u6a94\u3002<\/li>\n<li><strong>\u81ea\u52d5\u91cb\u653e\uff1a<\/strong>\u7576\u6e38\u6a19\u4e0d\u518d\u88ab\u4f7f\u7528\u6642\uff0cMongoDB\u6703\u81ea\u52d5\u91cb\u653e\u76f8\u95dc\u8cc7\u6e90\u3002<\/li>\n<li><strong>\u652f\u6301\u6392\u5e8f\u548c\u9650\u5236\uff1a<\/strong>\u7528\u6236\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u6307\u5b9a\u6392\u5e8f\u548c\u9650\u5236\u689d\u4ef6\uff0c\u6e38\u6a19\u6703\u6839\u64da\u9019\u4e9b\u689d\u4ef6\u8fd4\u56de\u7d50\u679c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%b8%b8%e6%a8%99-XFbmBneemt\">\u5982\u4f55\u4f7f\u7528\u6e38\u6a19<\/h2>\n<p>\u5728MongoDB\u4e2d\uff0c\u4f7f\u7528\u6e38\u6a19\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e6%9f%a5%e8%a9%a2-XFbmBneemt\">\u57fa\u672c\u67e5\u8a62<\/h3>\n<pre><code>const MongoClient = require('mongodb').MongoClient;\nconst url = 'mongodb:\/\/localhost:27017';\nconst dbName = 'mydatabase';\n\nMongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) =&gt; {\n    if (err) throw err;\n    const db = client.db(dbName);\n    const cursor = db.collection('mycollection').find();\n\n    cursor.forEach(doc =&gt; {\n        console.log(doc);\n    }, err =&gt; {\n        if (err) throw err;\n        client.close();\n    });\n});<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230MongoDB\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u4f7f\u7528 <code>find()<\/code> \u65b9\u6cd5\u67e5\u8a62\u96c6\u5408\u4e2d\u7684\u6240\u6709\u6587\u6a94\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528 <code>forEach<\/code> \u65b9\u6cd5\u904d\u6b77\u6e38\u6a19\uff0c\u9010\u4e00\u6253\u5370\u6bcf\u500b\u6587\u6a94\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e6%8e%92%e5%ba%8f%e5%92%8c%e9%99%90%e5%88%b6-XFbmBneemt\">\u4f7f\u7528\u6392\u5e8f\u548c\u9650\u5236<\/h3>\n<pre><code>const cursor = db.collection('mycollection').find().sort({ age: 1 }).limit(5);<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u4e86\u6392\u5e8f\uff0c\u6839\u64da <code>age<\/code> \u5b57\u6bb5\u7684\u503c\u5347\u5e8f\u6392\u5217\uff0c\u4e26\u9650\u5236\u8fd4\u56de\u7684\u6587\u6a94\u6578\u91cf\u70ba5\u3002<\/p>\n<h2 id=\"%e6%b8%b8%e6%a8%99%e7%9a%84%e5%85%b6%e4%bb%96%e6%93%8d%e4%bd%9c-XFbmBneemt\">\u6e38\u6a19\u7684\u5176\u4ed6\u64cd\u4f5c<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u67e5\u8a62\u64cd\u4f5c\uff0c\u6e38\u6a19\u9084\u652f\u6301\u5176\u4ed6\u4e00\u4e9b\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>toArray\uff1a<\/strong>\u5c07\u6e38\u6a19\u4e2d\u7684\u6240\u6709\u6587\u6a94\u8f49\u63db\u70ba\u6578\u7d44\u3002<\/li>\n<li><strong>count\uff1a<\/strong>\u8fd4\u56de\u6e38\u6a19\u4e2d\u5339\u914d\u7684\u6587\u6a94\u6578\u91cf\u3002<\/li>\n<li><strong>next\uff1a<\/strong>\u8fd4\u56de\u6e38\u6a19\u4e2d\u7684\u4e0b\u4e00\u500b\u6587\u6a94\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8toarray-XFbmBneemt\">\u793a\u4f8b\uff1a\u4f7f\u7528toArray<\/h3>\n<pre><code>cursor.toArray((err, docs) =&gt; {\n    if (err) throw err;\n    console.log(docs);\n});<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u6e38\u6a19\u4e2d\u7684\u6240\u6709\u6587\u6a94\u8f49\u63db\u70ba\u6578\u7d44\uff0c\u4e26\u6253\u5370\u51fa\u4f86\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XFbmBneemt\">\u7e3d\u7d50<\/h2>\n<p>\u6e38\u6a19\u5728MongoDB\u4e2d\u626e\u6f14\u8457\u91cd\u8981\u7684\u89d2\u8272\uff0c\u63d0\u4f9b\u4e86\u4e00\u7a2e\u9ad8\u6548\u7684\u65b9\u5f0f\u4f86\u8a2a\u554f\u548c\u64cd\u4f5c\u67e5\u8a62\u7d50\u679c\u3002\u901a\u904e\u7406\u89e3\u6e38\u6a19\u7684\u7279\u6027\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u9748\u6d3b\u5730\u8655\u7406\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u4e2d\u7684\u6e38\u6a19\u8853\u8a9e\uff0c\u638c\u63e1\u5982\u4f55\u6709\u6548\u5730\u67e5\u8a62\u548c\u64cd\u4f5c\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u5eab\u7ba1\u7406\u6280\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-63836","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=63836"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63836\/revisions"}],"predecessor-version":[{"id":63837,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63836\/revisions\/63837"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}