{"id":37,"date":"2024-01-28T11:31:44","date_gmt":"2024-01-28T03:31:44","guid":{"rendered":"https:\/\/blog.silmoon.dev\/?p=37"},"modified":"2024-01-28T11:31:45","modified_gmt":"2024-01-28T03:31:45","slug":"javascript%ef%bc%88nodejs%ef%bc%89%e8%af%bb%e5%8f%96%e5%92%8c%e5%86%99%e5%85%a5mongodb%e7%9a%84%e7%ae%80%e5%8d%95%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.silmoon.dev\/?p=37","title":{"rendered":"javascript\uff08nodejs\uff09\u8bfb\u53d6\u548c\u5199\u5165MongoDB\u7684\u7b80\u5355\u793a\u4f8b"},"content":{"rendered":"\n<p>\u9700\u8981\u5b89\u88c5mongodb\u5305\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install mongodb<\/code><\/pre>\n\n\n\n<p>\u76f8\u5173\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { MongoClient } = require('mongodb');\r\n\r\nconst client = new MongoClient('mongodb:\/\/\u7528\u6237\u540d:\u5bc6\u7801@\u670d\u52a1\u5668\/\u6570\u636e\u5e93');\r\n\r\nasync function run() {\r\n    await client.connect();\r\n    const database = client.db('test');\r\n    const collection = database.collection('users');\r\n    await collection.insertOne({ name: 'myName123', password: '123' });\r\n    const docs = await collection.find({'name': 'myName123'}).toArray();\r\n    console.log(docs);\r\n    client.close();\r\n}\r\n\r\n\r\nrun().catch(console.dir);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981\u5b89\u88c5mongodb\u5305\uff1a \u76f8\u5173\u4ee3\u7801\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,5],"tags":[6,7],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-8","category-5","tag-javascript","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silmoon.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}