[{"data":1,"prerenderedAt":219},["ShallowReactive",2],{"content:\u002Fposts\u002F2024\u002Fmongodb-setup":3,"surround:\u002Fposts\u002F2024\u002Fmongodb-setup":207},{"id":4,"title":5,"body":6,"canonical":184,"categories":185,"date":187,"description":188,"draft":189,"extension":190,"image":191,"meta":192,"navigation":193,"path":194,"permalink":184,"readingTime":195,"recommend":184,"references":184,"seo":200,"sitemap":201,"stem":202,"tags":203,"type":184,"updated":187,"__hash__":206},"content\u002Fposts\u002F2024\u002Fmongodb-setup.md","mongodb快速使用",{"type":7,"value":8,"toc":162},"minimark",[9,13,28,32,36,47,50,56,59,67,71,75,81,85,91,94,100,103,109,112,118,121,128,131,138,150,153],[10,11,12],"h2",{"id":12},"安装",[14,15,16,23],"p",{},[17,18,22],"a",{"href":19,"rel":20},"https:\u002F\u002Fwww.mongodb.com\u002F",[21],"nofollow","mongodb 官网",[17,24,27],{"href":25,"rel":26},"https:\u002F\u002Fwww.mongodb.com\u002Fzh-cn",[21],"mongodb 中文官网",[10,29,31],{"id":30},"node-实例-使用-express-和-mongoose-实现简单的文章管理-api","node 实例: 使用 express 和 mongoose 实现简单的文章管理 API",[33,34,35],"h3",{"id":35},"初始化项目",[37,38,44],"pre",{"className":39,"code":41,"language":42,"meta":43},[40],"language-bash","npm init -y\n","bash","",[45,46,41],"code",{"__ignoreMap":43},[33,48,49],{"id":49},"依赖安装",[37,51,54],{"className":52,"code":53,"language":42,"meta":43},[40],"npm i express@latest cors mongoose\n",[45,55,53],{"__ignoreMap":43},[33,57,58],{"id":58},"数据建模",[37,60,65],{"className":61,"code":63,"language":64,"meta":43},[62],"language-js","import mongoose from \"mongoose\"\n\nconst Article = mongoose.model(\n    \"Article\",\n    new mongoose.Schema({\n        title: { type: String },\n    })\n)\n","js",[45,66,63],{"__ignoreMap":43},[33,68,70],{"id":69},"增删改查-api-实现","增删改查 API 实现",[72,73,74],"h4",{"id":74},"获取列表",[37,76,79],{"className":77,"code":78,"language":64,"meta":43},[62],"app.get(\"\u002Farticles\", async (req, res) => {\n    const article = await Article.find()\n    res.send({\n        success: true,\n        message: \"get article list\",\n        data: article,\n    })\n})\n",[45,80,78],{"__ignoreMap":43},[72,82,84],{"id":83},"根据-id-获取详情","根据 id 获取详情",[37,86,89],{"className":87,"code":88,"language":64,"meta":43},[62],"app.get(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    const article = await Article.findById(id)\n    res.send({\n        success: true,\n        message: \"get article by id\",\n        data: article,\n    })\n})\n",[45,90,88],{"__ignoreMap":43},[72,92,93],{"id":93},"创建文章",[37,95,98],{"className":96,"code":97,"language":64,"meta":43},[62],"app.post(\"\u002Farticles\", async (req, res) => {\n    const body = req.body\n\n    const article = await Article.create({\n        title: body.title,\n    })\n\n    res.send({\n        success: true,\n        message: \"create article\",\n        data: article,\n    })\n})\n",[45,99,97],{"__ignoreMap":43},[72,101,102],{"id":102},"编辑文章",[37,104,107],{"className":105,"code":106,"language":64,"meta":43},[62],"app.put(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    const body = req.body\n\n    const article = await Article.findById(id)\n    article.title = body.title\n    await article.save()\n\n    res.send({\n        success: true,\n        message: \"update article\",\n        data: article,\n    })\n})\n",[45,108,106],{"__ignoreMap":43},[72,110,111],{"id":111},"删除文章",[37,113,116],{"className":114,"code":115,"language":64,"meta":43},[62],"app.delete(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    await Article.deleteOne({ _id: id })\n\n    \u002F\u002F 或者可以使用以下方法进行删除\n    \u002F\u002F const article = await Article.findById(id)\n    \u002F\u002F await article.remove()\n\n    res.send({\n        success: true,\n        message: \"delete article\",\n    })\n})\n",[45,117,115],{"__ignoreMap":43},[10,119,120],{"id":120},"完整代码",[37,122,126],{"className":123,"code":124,"language":64,"meta":125},[62],"import cors from \"cors\"\nimport express from \"express\"\nimport mongoose from \"mongoose\"\n\nconst app = express()\napp.use(express.json())\napp.use(cors())\n\ntry {\n    await mongoose.connect(\"mongodb:\u002F\u002F127.0.0.1:27017\u002Fmongodb-test\")\n}\ncatch (error) {\n    console.error(\"error:数据库连接失败\\n\", error)\n}\n\nconst Article = mongoose.model(\n    \"Article\",\n    new mongoose.Schema({\n        title: { type: String },\n    })\n)\n\napp.get(\"\u002Farticles\", async (req, res) => {\n    const article = await Article.find()\n    res.send({\n        success: true,\n        message: \"get article list\",\n        data: article,\n    })\n})\n\napp.get(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    const article = await Article.findById(id)\n    res.send({\n        success: true,\n        message: \"get article by id\",\n        data: article,\n    })\n})\n\napp.post(\"\u002Farticles\", async (req, res) => {\n    const body = req.body\n\n    const article = await Article.create({\n        title: body.title,\n    })\n\n    res.send({\n        success: true,\n        message: \"create article\",\n        data: article,\n    })\n})\n\napp.put(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    const body = req.body\n\n    const article = await Article.findById(id)\n    article.title = body.title\n    await article.save()\n\n    res.send({\n        success: true,\n        message: \"update article\",\n        data: article,\n    })\n})\n\napp.delete(\"\u002Farticles\u002F:id\", async (req, res) => {\n    const id = req.params.id\n    await Article.deleteOne({ _id: id })\n    res.send({\n        success: true,\n        message: \"delete article\",\n    })\n})\n\nconst port = 3000\napp.listen(port, () => {\n    console.log(`http:\u002F\u002Flocalhost:${port}`)\n})\n","main.js",[45,127,124],{"__ignoreMap":43},[72,129,130],{"id":130},"查看运行情况",[14,132,133,134,137],{},"可以使用 vscode 插件 rest client 来测试接口运行情况，以下是我本地接口测试文件。如果你要在本地测试，可以将 ",[45,135,136],{"code":136},"66ae4047563fa3933c1b38ba"," 改成真实的查询列表里的某一个文章的 id 即可",[139,140,141],"alert",{},[14,142,143,144,149],{},"rest client 这个插件在我的博客里有介绍，可以移步 ",[17,145,148],{"href":146,"rel":147},"https:\u002F\u002Fwww.weilog.me\u002F2023\u002F01\u002F03\u002Frest-client%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8\u002F",[21],"rest client 插件使用"," 了解其基础使用方法",[72,151,152],{"id":152},"测试代码",[37,154,160],{"className":155,"code":157,"language":158,"meta":159},[156],"language-http","@uri=http:\u002F\u002Flocalhost:3000\n\n### 获取文章列表\nGET {{uri}}\u002Farticles\n\n### 创建文章\nPOST {{uri}}\u002Farticles\nContent-Type: application\u002Fjson\n\n{\n  \"title\": \"添加文章标题\"\n}\n\n### 根据id获取文章\nGET {{uri}}\u002Farticles\u002F66ae4047563fa3933c1b38ba\n\n### 编辑文章\nPUT {{uri}}\u002Farticles\u002F66ae4047563fa3933c1b38ba\nContent-Type: application\u002Fjson\n\n{\n  \"title\": \"编辑文章标题\"\n}\n\n### 删除文章\nDELETE {{uri}}\u002Farticles\u002F66ae4047563fa3933c1b38ba\n","http","articles.http",[45,161,157],{"__ignoreMap":43},{"title":43,"searchDepth":163,"depth":163,"links":164},4,[165,167,180],{"id":12,"depth":166,"text":12},2,{"id":30,"depth":166,"text":31,"children":168},[169,171,172,173],{"id":35,"depth":170,"text":35},3,{"id":49,"depth":170,"text":49},{"id":58,"depth":170,"text":58},{"id":69,"depth":170,"text":70,"children":174},[175,176,177,178,179],{"id":74,"depth":163,"text":74},{"id":83,"depth":163,"text":84},{"id":93,"depth":163,"text":93},{"id":102,"depth":163,"text":102},{"id":111,"depth":163,"text":111},{"id":120,"depth":166,"text":120,"children":181},[182,183],{"id":130,"depth":163,"text":130},{"id":152,"depth":163,"text":152},null,[186],"代码","2024-08-01 22:28:48","使用 express + mongoose 快速开发API接口",false,"md","https:\u002F\u002Fbitmc.uno\u002Fpicgo\u002F679adc96a43ad-78.webp",{},true,"\u002Fposts\u002F2024\u002Fmongodb-setup",{"text":196,"minutes":197,"time":198,"words":199},"4 min read",3.1,186000,620,{"title":5,"description":188},{"loc":194},"posts\u002F2024\u002Fmongodb-setup",[204,205],"mongodb","学习笔记","6he0BrMAwogBFt1VK89P7lrxe1CSOuao9hdVvtAMhWg",[208,214],{"title":209,"path":210,"stem":211,"date":212,"type":213,"children":-1},"近况说明","\u002Fposts\u002F2024\u002Frecent-fact-sheet","posts\u002F2024\u002FRecent-fact-sheet","2024-07-14 13:14:55","story",{"title":215,"path":216,"stem":217,"date":218,"type":184,"children":-1},"发布订阅模式","\u002Fposts\u002F2024\u002Fevent-bus","posts\u002F2024\u002Fevent-bus","2024-08-16 23:50:01",1789642591632]