{"id":4532,"date":"2023-09-08T12:33:51","date_gmt":"2023-09-08T04:33:51","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=4532"},"modified":"2023-09-08T12:33:51","modified_gmt":"2023-09-08T04:33:51","slug":"%e8%87%aa%e5%bb%ba%e8%b5%84%e6%96%99%e5%ba%93app%e6%9c%89%e5%93%aa%e4%ba%9b%e6%ad%a5%e9%aa%a4%e5%91%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/4532\/","title":{"rendered":"\u81ea\u5efa\u8d44\u6599\u5e93app\u6709\u54ea\u4e9b\u6b65\u9aa4\u5462\uff1f"},"content":{"rendered":"

\u81ea\u5efa\u8d44\u6599\u5e93app\u662f\u6307\u7528\u6237\u5229\u7528\u73b0\u6709\u7684\u6280\u672f\u624b\u6bb5\uff0c\u901a\u8fc7\u81ea\u4e3b\u642d\u5efa\u6570\u636e\u5e93\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c06\u81ea\u5df1\u7684\u6570\u636e\u8fdb\u884c\u5206\u7c7b\u3001\u6574\u7406\u3001\u5b58\u50a8\u548c\u67e5\u8be2\u7b49\u64cd\u4f5c\u7684\u4e00\u79cd\u5e94\u7528\u3002\u4e00\u822c\u7528\u6237\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u6570\u636e\u5e93\u548c\u7f16\u7a0b\u5de5\u5177\uff0c\u81ea\u4e3b\u521b\u5efa\u5c5e\u4e8e\u81ea\u5df1\u7684\u6570\u636e\u5e93\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u800c\u8fbe\u5230\u66f4\u52a0\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u5229\u7528\u81ea\u5df1\u7684\u6570\u636e\u8d44\u6e90\u7684\u76ee\u7684\u3002<\/p>\n

\u5b9e\u73b0\u81ea\u5efa\u8d44\u6599\u5e93app\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\uff0c\u4e0b\u9762\u4e3b\u8981\u4ecb\u7ecd\u57fa\u4e8eNode.js\u548cMongoDB\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n

1. \u642d\u5efa\u73af\u5883\u548c\u5b89\u88c5\u8f6f\u4ef6<\/p>\n

\u642d\u5efa\u73af\u5883\u662f\u524d\u7f6e\u6761\u4ef6\uff0c\u9700\u8981\u7528\u6237\u62e5\u6709\u4e00\u53f0\u53ef\u4ee5\u8fd0\u884cNode.js\u4ee5\u53caMongoDB\u7684\u670d\u52a1\u5668\uff0c\u4e00\u822c\u53ef\u4ee5\u9009\u62e9\u4e91\u670d\u52a1\u5668\u3002<\/p>\n

\u5b89\u88c5\u8f6f\u4ef6\uff1aNode.js\u5b98\u7f51\u63d0\u4f9b\u4e86Windows\u3001macOS\u3001Linux\u7b49\u591a\u4e2a\u5e73\u53f0\u7684\u7248\u672c\uff0c\u7528\u6237\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u7248\u672c\u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002MongoDB\u5219\u9700\u8981\u4e0b\u8f7dCommunity Server\u7248\u672c\uff0c\u7136\u540e\u8fdb\u884c\u89e3\u538b\u7f29\uff0c\u5f97\u5230\u540d\u81ea\u5df1\u5efaAPP<\/a>\u4e3abin\u7684\u6587\u4ef6\u5939\u3002<\/p>\n

2. \u521b\u5efa\u6570\u636e\u5e93<\/p>\n

\u5728\u5b89\u88c5\u597dMongoDB\u4e4b\u540e\uff0c\u9700\u8981\u8fdb\u884c\u6570\u636e\u5e93\u7684\u521b\u5efa\u3002\u542f\u52a8MongoDB\u7684Command Prompt\uff0c\u8f93\u5165\uff1a<\/p>\n

“`<\/p>\n

$ mongo<\/p>\n

“`<\/p>\n

\u4f1a\u51fa\u73b0\u4e00\u4e2a\u547d\u4ee4\u884c\u754c\u9762\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2adatabase\uff1a<\/p>\n

“`<\/p>\n

> use mydb<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u6570\u636e\u5e93\u4e0b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e9b\u6570\u636e\u96c6\uff08collection\uff09\u3002\u6bd4\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u53ebmytable\u7684collection\uff1a<\/p>\n

“`<\/p>\n

> db.createCollecti<\/p>\n

<\/figure>\n<\/p>\n

on(“mytable”)<\/p>\n

“`<\/p>\n

\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u6240\u6709collection\u540d\u79f0\uff1a<\/p>\n

“`<\/p>\n

> show collections<\/p>\n

“`<\/p>\n

3. \u521b\u5efaNode.js\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u5728\u9009\u62e9Node.js\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u5e73\u53f0\u4e4b\u540e\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u6a21\u5757\uff0c\u7528\u4e8e\u8fde\u63a5MongoDB\u6570\u636e\u5e93\u548c\u7f16\u5199\u7a0b\u5e8f\u3002<\/p>\n

\u5728Node.js\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u76f8\u5173\u6a21\u5757\uff1a<\/p>\n

“`<\/p>\n

$ npm install –save express mongodb<\/p>\n

“`<\/p>\n

\u8fd9\u6837\u5c31\u5b89\u88c5\u4e86\u4e24\u4e2a\u6a21\u5757\uff0cexpress\u7528\u4e8e\u642d\u5efaWeb\u5e94\u7528\uff0cmongodb\u7528\u4e8e\u8fde\u63a5MongoDB\u6570\u636e\u5e93\u3002\u73b0\u5728\uff0c\u5f00\u59cb\u7f16\u5199Node.js\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.js\u7684\u6587\u4ef6\uff0c\u5728\u91cc\u9762\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`js<\/p>\n

const express = require(‘express’);<\/p>\n

const MongoClient = require(‘mongodb’).MongoClient;<\/p>\n

const app = express();<\/p>\n

let dbo; \/\/ \u5168\u5c40\u53d8\u91cf\uff0c\u4fdd\u5b58db\u5bf9\u8c61<\/p>\n

app.get(‘\/all’, async (req, res) => {<\/p>\n

const collection = dbo.collection(“mytabl\u81ea\u5efaAPP<\/a>e”);<\/p>\n

const result = await collection.find({}).toArray();<\/p>\n

res.send(result);<\/p>\n

});<\/p>\n

MongoClient.connect(“mongodb:\/\/localhost:27017”, { useNewUrlParser: true }, (err, db) => {<\/p>\n

if (err) throw err;<\/p>\n

dbo = db.db(“mydb”);<\/p>\n

app.listen(3000, () => console.log(‘Example app listening on port 3000!’))<\/p>\n

});<\/p>\n

“`<\/p>\n

\u8fd9\u6bb5\u4ee3\u7801\u505a\u4e86\u4ee5\u4e0b\u51e0\u4ef6\u4e8b\u60c5\uff1a<\/p>\n

1. \u5f15\u5165express\u548cmongodb\u6a21\u5757\u3002<\/p>\n

2. \u521b\u5efa\u4e00\u4e2a\u540d\u4e3aapp\u7684Express\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

3. \u5168\u5c40\u53d8\u91cfdbo\uff0c\u7528\u4e8e\u4fdd\u5b58MongoDB\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n

4. \u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\uff0c\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n

5. \u8fde\u63a5MongoDB\u6570\u636e\u5e93\uff0c\u5e76\u542f\u52a8Web\u670d\u52a1\u76d1\u542c3000\u7aef\u53e3\u3002<\/p>\n

4. \u8fd0\u884c\u5e76\u4f7f\u7528<\/p>\n

\u5728\u5b8c\u6210\u4ee5\u4e0a\u6587\u4ef6\u7684\u7f16\u5199\u4e4b\u540e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\uff1a<\/p>\n

“`<\/p>\n

$ node index.js<\/p>\n

“`<\/p>\n

\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u4e86`Example app listening on port 3000!`\uff0c\u8868\u793a\u670d\u52a1\u5df2\u7ecf\u542f\u52a8\u3002\u6b64\u65f6\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee`http:\/\/localhost:3000\/all`\uff0c\u5373\u53ef\u770b\u5230\u6240\u6709\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n

\u81f3\u6b64\uff0c\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5efa\u8d44\u6599\u5e93app\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u7684\u5b9e\u9645\u9700\u6c42\uff0c\u8fdb\u884c\u66f4\u52a0\u7ec6\u81f4\u7684\u7f16\u5199\u3001\u8bbe\u8ba1\u548c\u4f18\u5316\u3002\u8be5\u5e94\u7528\u53ef\u4ee5\u8ba9\u7528\u6237\u66f4\u597d\u5730\u7ba1\u7406\u81ea\u5df1\u7684\u6570\u636e\uff0c\u63d0\u9ad8\u4fe1\u606f\u5229\u7528\u6548\u7387\uff0c\u800c\u4e14\u6709\u5f88\u5927\u7684\u6269\u5c55\u7a7a\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u81ea\u5efa\u8d44\u6599\u5e93app\u662f\u6307\u7528\u6237\u5229\u7528\u73b0\u6709\u7684\u6280\u672f\u624b\u6bb5\uff0c\u901a\u8fc7\u81ea\u4e3b\u642d\u5efa\u6570\u636e\u5e93\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u5c06\u81ea\u5df1\u7684\u6570\u636e\u8fdb\u884c\u5206\u7c7b\u3001\u6574\u7406\u3001\u5b58\u50a8\u548c\u67e5\u8be2\u7b49\u64cd\u4f5c\u7684\u4e00\u79cd\u5e94\u7528\u3002\u4e00\u822c\u7528\u6237\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u6570\u636e\u5e93\u548c\u7f16\u7a0b\u5de5\u5177\uff0c\u81ea\u4e3b\u521b\u5efa\u5c5e\u4e8e<\/p>\n","protected":false},"author":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[168,369,8766,8765,75],"topic":[],"class_list":{"0":"post-4532","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-appkf","7":"tag-168","8":"tag-369","9":"tag-app","11":"tag-75"},"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/4532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=4532"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/4532\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=4532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=4532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=4532"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=4532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}