{"id":5741,"date":"2017-04-10T18:21:08","date_gmt":"2017-04-10T10:21:08","guid":{"rendered":"https:\/\/sdeno.com\/?p=5741"},"modified":"2023-01-20T14:37:21","modified_gmt":"2023-01-20T06:37:21","slug":"%e6%8e%a8%e8%8d%90%ef%bc%9akoa2%e4%b8%8emongodb%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5741","title":{"rendered":"\u63a8\u8350\uff1akoa2\u4e0emongodb\u4ea4\u4e92"},"content":{"rendered":"<p>\u5982\u679c\u662f\u4f7f\u7528mongodb\u4e0enode.js\u914d\u5408\u4f7f\u7528\uff0c\u8fd9\u63a8\u8350\u5b89\u88c5monk\u6a21\u5757\uff0c\u4e0d\u7ba1\u662fkoa 2\u8fd8\u662fexpress 4.x\u90fd\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<p>\u8be6\u7ec6\u6587\u6863\uff1a<a href=\"https:\/\/automattic.github.io\/monk\/docs\/GETTING_STARTED.html\" target=\"_blank\" rel=\"noopener\">https:\/\/automattic.github.io\/monk\/docs\/GETTING_STARTED.html<\/a><\/p>\n<p><span style=\"color: #ff0000;\">\u5b89\u88c5\uff1a<\/span><\/p>\n<pre>npm install --save monk<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u8c03\u7528\uff1a<\/span><\/p>\n<pre>const monk = require('monk');\r\nconst url = 'localhost:27017\/xgllseo';\r\nconst db = monk(url);<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u81ea\u52a8\u9012\u589e\uff1a<\/span><\/p>\n<p>\u5148\u6267\u884c\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u4e13\u95e8\u7528\u6765\u5b58\u50a8\u81ea\u589e\u6570\u5b57<\/p>\n<pre>db.createCollection(\"counters\")\r\ndb.counters.insert({_id:\"productid\",sequence_value:1})<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6279\u91cf\u63d2\u5165\u6570\u636e\u7684\u540c\u65f6\uff0c\u9012\u589e\u7d22\u5f15<\/p>\n<pre>const collection = db.get('admin'); \/\/admin\u662f\u96c6\u5408\uff0c\u6839\u636e\u9700\u6c42\u4fee\u6539\u6210\u81ea\u5df1\u7684\u96c6\u5408\r\ndb.get('counters').find({}).then((docs) =&gt; {  \/\/1\uff0c\u5148\u67e5\u8be2\u5f53\u524d\u6700\u65b0\u7684\u81ea\u589e\u6570\u503c\u662f\u591a\u5c11\r\n   var _id=docs[0].sequence_value;\r\n   function autoadd() {\r\n      return ++_id;   \/\/2\uff0c\u5f53\u524d\u6700\u65b0\u7684\u81ea\u589e\u6570\uff0c\u6bcf\u6b21\u90fd\u7d2f\u52a01\r\n   }\r\n   collection.insert([\r\n     {\"_id\":autoadd(),user: ctx.request.body.user,pass: ctx.request.body.pass},  \/\/3\uff0c\u6bcf\u63d2\u5165\u4e00\u6761\u6570\u636e\uff0c\u7d22\u5f15\u5c31\u7d2f\u52a01\u4e86\r\n     {\"_id\":autoadd(),user:'a123',pass:'456'},\r\n     {\"_id\":autoadd(),user:'b123',pass:'456'},\r\n     {\"_id\":autoadd(),user:'c123',pass:'456'},\r\n     {\"_id\":autoadd(),user:'d123',pass:'456'}]\r\n   ).then((doc)=&gt;{\r\n     obj.aa=doc;\r\n     \/\/4,\u63d2\u5165\u7684\u6570\u636e\u7ed3\u675f\u540e\uff0c\u66f4\u65b0\u4e0b\u5b58\u50a8\u7684\u81ea\u589e\u6570\u7684\u96c6\u5408\uff0c\u65b9\u4fbf\u4e0b\u6b21\u63d2\u5165\u6570\u636e\u65f6\uff0c\u63a5\u7740\u6700\u65b0\u7684\u7d22\u5f15\u3002\r\n     db.get('counters').findOneAndUpdate({sequence_value: (doc[0][\"_id\"])-1}, {sequence_value: doc[doc.length-1][\"_id\"]}).then(()=&gt;{a();})\r\n   });\r\n});<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><span style=\"color: #ff0000;\">\u589e<\/span><\/p>\n<pre>\/\/\u4e3a\u4e86\u96c6\u5408\u6dfb\u52a0\u4e00\u6761\u6587\u6863\r\ndb.get('\u96c6\u5408').insert([\r\n {\r\n   \"_id\":autoadd(), \/\/\u4e5f\u53ef\u4ee5\u6267\u884c\u51fd\u6570\r\n   post_title: filter.html2Escape(ctx.request.body.post_title),\r\n   post_con: ctx.request.body.post_con,\r\n   category:ctx.request.body.category,\r\n   time:moment().format('YYYY-MM-DD kk:mm:ss')\r\n }\r\n])\r\n\r\n\/\/\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u6587\u6863\r\ndb.get('\u96c6\u5408').insert([{ woot: 'bar' }, { woot: 'baz' }])<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5220<\/span><\/p>\n<pre>db.get('\u96c6\u5408').remove()  \/\/\u5220\u9664\u67d0\u96c6\u5408\u4e0b\u91cc\u9762\u7684\u6240\u6709\u6587\u6863\r\ndb.get('\u96c6\u5408').remove({\"_id\":1});  \/\/\u6839\u636e\u6761\u4ef6\u5220\u9664_id=1\u7684\u6587\u6863<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u6539<\/span><\/p>\n<pre>db.get('\u96c6\u5408').update({name: '123'}, {$set :{num:456}}) \/\/\u627e\u5230\u96c6\u5408\u4e0b\u542bname=123\u7684\u4e00\u4e2a\u6761\u6587\u6863\uff0c\u4fee\u6539\u5b83\u5b57\u6bb5num\u503c\u4e3a456<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">db.get('\u96c6\u5408').update(\r\n  { type : \"book\" },\r\n  { $inc : { qty : 1 } },  \/\/$inc\u662f\u6570\u5b57\u7d2f\u52a0\r\n  { multi: true }\r\n)\u00a0 \u00a0 \/\/\u627e\u5230\u96c6\u5408\u91cc\u9762\u6240\u6709type=book\u7684\u6587\u6863\uff0c\u8ba9\u5404\u81ea\u7684\u6587\u6863\u4e2dqty\u7684\u5b57\u6bb5\u7d2f\u52a01<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u67e5<\/span><\/p>\n<pre>db.get('\u96c6\u5408').find({}).then((doc)=&gt;{})  \/\/\u83b7\u53d6\u5230\u96c6\u5408\u7684\u6240\u6709\u6587\u6863\r\ndb.get('\u96c6\u5408').find({\"_id\":1}).then((doc)=&gt;{})  \/\/\u53ea\u83b7\u53d6\u6307\u5b9aid\u7684\u6587\u6863\r\n\r\n\/\/\u6a21\u7cca\u641c\u7d22,\u641c\u7d22\u96c6\u5408\u4e2d\u7684post_title\u5b57\u6bb5\u6216\u8005post_con\u5b57\u6bb5\uff0c\u82e5\u542b\u6709\u6307\u5b9a\u5173\u952e\u8bcd\uff0c\u5c31\u9009\u53d6\u51fa\u6765\u76f8\u5e94\u7684\u6587\u6863\r\ndb.get('\u96c6\u5408').find({$or:[{\"post_title\":new RegExp(ctx.query['search[value]'],'ig')},{\"post_con\": new RegExp(ctx.query['search[value]'],'ig')}]})\r\n\r\n\/\/\u6a21\u7cca\u641c\u7d22\uff0c\u641c\u7d22post\u96c6\u5408\uff0c\u5728\u96c6\u5408\u7684\u5b57\u6bb5post_title\u6216\u8005post_con\u5b57\u6bb5\u4e2d\u5982\u679c\u542b\u6709\bvalue\u5173\u952e\u8bcd,\u5c31\u9009\u51fa\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\uff0c\r\n\u5e76\u4e14\u4ee5\u5b57\u6bb5_id\u7684\u4e3a\u51c6\u964d\u5e8f\u6392\u5217(\u6570\u5b57\u5927\u5230\u5c0f)\uff0c\u5ffd\u7565\u524d10\u6761\u6570\u636e\uff0c\u4ece\u7b2c11\u6761\u6570\u636e\u5f00\u59cb\uff0c\u53ea\u663e\u793a5\u6761\u6570\u636e\uff0c\u6bcf\u6761\u6570\u636e\u4e0d\u663e\u793apost_con\u5b57\u6bb5\u3002\r\ndb.get('post').find({$or:[{\"post_title\":new RegExp(ctx.query['search[value]'],'ig')},{\"post_con\": new RegExp(ctx.query['search[value]'],'ig')}]}, \r\n {\r\n   fields: { post_con: 0 },\r\n   sort: {\"_id\": -1},\r\n   limit: 5,\r\n   skip: 10\r\n})\r\n\r\n\r\ndb.get('\u96c6\u5408').count()  \/\/\u6761\u6570\r\ndb.get('\u96c6\u5408').count({name: 'foo'})  \/\/\u8fd4\u56de\u5b57\u6bb5name\u7684\u503c\u662ffoo\u7684\u6240\u6709\u6587\u6863\u6761\u6570\r\n\r\nfindOne()  \/\/\u4e0d\u7ba1\u6570\u636e\u591a\u5c11\uff0c\u53ea\u663e\u793a\u4e00\u6761\u6570\u636e\r\nfindOneAndDelete()  \/\/\u4e0d\u7ba1\u6570\u636e\u591a\u5c11\uff0c\u53ea\u627e\u5230\u4e00\u6761\u6570\u636e\u5e76\u5220\u9664\r\nfindOneAndUpdate()<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5173\u95ed\u4e0e\u6570\u636e\u5e93\u8fde\u63a5<\/span><\/p>\n<pre>db.close()<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u662f\u4f7f\u7528mongodb\u4e0enode.js\u914d\u5408\u4f7f\u7528\uff0c\u8fd9\u63a8\u8350\u5b89\u88c5monk\u6a21\u5757\uff0c\u4e0d\u7ba1\u662fkoa 2\u8fd8\u662fexpress  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-5741","post","type-post","status-publish","format-standard","hentry","category-koa"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5741"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5741\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}