{"id":163041,"date":"2020-10-03T03:10:51","date_gmt":"2020-10-02T19:10:51","guid":{"rendered":"http:\/\/4563.org\/?p=163041"},"modified":"2020-10-03T03:10:51","modified_gmt":"2020-10-02T19:10:51","slug":"mongoose-%e5%a6%82%e4%bd%95%e6%9b%b4%e6%96%b0%e5%86%85%e5%b5%8c%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1%e7%9a%84%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=163041","title":{"rendered":"mongoose \u5982\u4f55\u66f4\u65b0\u5185\u5d4c\u6570\u7ec4\u5bf9\u8c61\u7684\u6570\u636e\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  mongoose \u5982\u4f55\u66f4\u65b0\u5185\u5d4c\u6570\u7ec4\u5bf9\u8c61\u7684\u6570\u636e\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ideacco <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6298\u817e\u4e86\u4e00\u665a\u4e0a\uff0c\u6587\u6863\u770b\u5b8c\u4e86\uff0c\u8c37\u6b4c\u64cd\u4f5c\u4e00\u987f\uff0c\u6ca1\u641e\u5b9a\u3002\u53ea\u80fd\u6c42\u52a9\u5927\u795e\u5566\u3002<\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u6570\u636e\u8868 Schema \u5927\u6982\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ PostCountry \u8868 const PostCountrySchema = new Schema({   uid: {     type: ObjectId,     ref: 'User',     index: true,     required: true   },   pid: {     type: String,     ref: 'Product',     index: { unique: true }, \/\/ \u8bbe\u4e3a\u552f\u4e00\u7684     required: true   },   country_list: [     {       country_name: {         type: String,         required: true       },       country_code: String,       fourPx_code: {         type: String,         default: ''       },       declared: { \/\/ \u7533\u62a5\u5355\u4ef7         type: Number,         default: 0       },       fourPx_send_name: {         type: String,         default: ''       }     }   ] }   <\/code><\/pre>\n<p>\u6570\u636e\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ PostCountry \u8868  \"country_list\": [         {             \"country_name\": \"China\",             \"declared\": 3,             \"fourPx_send_name\": \"\u4e2d\u56fd\u90ae\u653f\"         },         {             \"country_name\": \"Ecuador\",             \"declared\": 21,             \"fourPx_send_name\": \"\u4e2d\u56fd\u90ae\u653f\"         },         {                \"country_name\": \"Argentina\",             \"declared\": 14,             \"fourPx_send_name\": \"\u4e2d\u56fd\u90ae\u653f\"         }     ],     \"pid\": \"5380578836639\",     \"uid\": \"5f7205b75e08ebr1dab5abfa\"   <\/code><\/pre>\n<p>\u6211\u73b0\u5728\u9700\u8981\u66f4\u65b0 PostCountry \u8868\u4e2d\u5d4c\u5957\u6570\u7ec4\u7684\uff0cfourPx_send_name \u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u5b58\u5728\u5219\u66f4\u65b0\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u5728\u8fd9\u4e2a\u8868\u5185\u589e\u52a0\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u6211 post \u586b\u5145\u7684\u6570\u636e\u662f\uff1a<\/p>\n<pre><code>{     \"country_name\": \"Canada\",     \"declared\": 123,     \"fourPx_send_name\": \"\u4e2d\u56fd\u90ae 31231 \u653f\",     \"pid\":\"5380578836639\" }  <\/code><\/pre>\n<p>\u6211\u7528\u8fc7\u7684\u65b9\u6cd5 1\uff1a<\/p>\n<pre><code>const Data = await PostCountrySchema.findOneAndUpdate({       uid: ctx.cookies.get('uid'),       pid: psotData.pid,       country_list: {         $elemMatch: {           country_name: psotData.country_name         }       }     },     {       $set: {         'country_list.$.fourPx_send_name': psotData.fourPx_send_name,         'country_list.$.declared': psotData.declared       }     },     { new: true, upsert: true, setDefaultsOnInsert: true })       .then((data) =&gt; {         return data       })       .catch(err =&gt; console.error('\u8fd4\u56de\u9519\u8bef' + err))  <\/code><\/pre>\n<p>\u8fd9\u6837\u64cd\u4f5c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef <code>MongoError: Updating the path 'country_list.$.fourPx_send_name' would create a conflict at 'country_list'<\/code><\/p>\n<p>\u6211\u7528\u8fc7\u7684\u65b9\u6cd5 2\uff1a<\/p>\n<pre><code>const Data = await PostCountrySchema.findOneAndUpdate({       uid: ctx.cookies.get('uid'),       pid: psotData.pid,       'country_list.country_name': { $ne: psotData.country_name }     },     {        $addToSet: {         country_list: {           country_name: psotData.country_name,           fourPx_send_name: psotData.fourPx_send_name,           declared: psotData.declared         }       }     },     { new: true, upsert: true, setDefaultsOnInsert: true })       .then((data) =&gt; {         return data       })       .catch(err =&gt; console.error('\u8fd4\u56de\u9519\u8bef' + err))  <\/code><\/pre>\n<p>\u8fd9\u6837\u64cd\u4f5c\uff0c\u5982\u679c country_name \u5b57\u6bb5\u540d\u5b57\u6ca1\u641c\u7d22\u5230\uff0c\u662f\u53ef\u4ee5\u6dfb\u52a0\u6210\u529f\u7684\uff0c\u7136\u800c\u518d\u6b21\u63d0\u4ea4\u4fee\u6539\u5c31\u4f1a\u62a5\u9519\u4e86\uff1a<\/p>\n<p><code>MongoError: E11000 duplicate key error collection: shopify.post_country index: pid_1 dup key: { pid: \"5380578836639\" }<\/code><\/p>\n<p>\u64cd\u4f5c 3\uff0c\u5c06 \u4e0a\u9762\u7684 $addToSet \u6539\u6210$push \u7ed3\u679c\u4e5f\u662f\u4e00\u6837\uff0c\u6ca1\u6709\u6570\u636e\u53ef\u4ee5\u586b\u5145\uff0c\u6709\u6570\u636e\u5c31\u65e0\u6cd5\u586b\u5145\u4e86\u3002<\/p>\n<h3>\u5982\u4f55\u624d\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u66f4\u65b0\u5185\u5d4c\u6570\u7ec4\u7684\u6570\u636e\uff0c\u5982\u65e0\u6570\u636e\u5219\u65b0\u589e\u6570\u7ec4\uff0c\u5982\u679c\u6709\u6570\u636e\u5219\u66f4\u65b0\u6570\u7ec4\uff1f<\/h3>\n<p>\u6298\u78e8\u4e86\u534a\u5929\u4e86\uff0c\u6c42\u5927\u795e\u652f\u4e2a\u62db\u5427\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3553071\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/docs.mongodb.com\/manual\/reference\/operator\/update\/addToSet\/#value-to-add-is-a-document<br \/>$addToSet \u5fc5\u987b\u5f97\u662f\u5b57\u6bb5\u548c\u503c\u5b8c\u5168\u4e00\u6837\u624d\u4f1a\u8ba4\u4e3a\u4e0d\u9700\u8981\u63d2\u5165\uff0c\u4e5f\u5c31\u662f\u8bf4\u9876\u591a\u505a\u63d2\u5165\uff0c\u8ddf\u66f4\u65b0\u6570\u636e\u6ca1\u5173\u7cfb\u3002<\/p>\n<p>\u4f60\u8981\u662f\u975e\u8981\u6309\u7167\u73b0\u6709\u7684 schema \u6765\u505a\uff0c\u53ea\u80fd\u7528\u7a0b\u5e8f\u5148\u904d\u5386 country_list\uff0c\u627e\u5230\u4e00\u6837\u7684 fourPx_send_name \u5c31\u4fee\u6539\u6570\u636e\uff0c\u7136\u540e\u6574\u4e2a document save \u3002<\/p>\n<p>MongoDB \u64cd\u4f5c\u7684\u6700\u5c0f\u5355\u4f4d\u662f\u6587\u6863(Document)\uff0c\u4f60\u73b0\u5728\u64cd\u4f5c\u7684\u662f\u5b50\u6587\u6863\uff0c\u6240\u4ee5\u4f1a\u5f88\u9ebb\u70e6\u3002<\/p>\n<p>\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u5bf9\u4f60\u73b0\u5728\u64cd\u4f5c\u7684\u662f\u5b50\u6587\u6863\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u5e94\u8be5\u628a\u4f60\u73b0\u5728\u64cd\u4f5c\u7684\u662f\u5b50\u6587\u6863\u62bd\u51fa\u6765\u505a\u6210\u4e00\u4e2a\u65b0\u7684 Collection\uff0c\u7136\u540e\u7528 ref \u548c populate \u6765\u4e0e PostCountry \u5efa\u7acb\u5173\u7cfb\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553072\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u64cd\u4f5c\u5b50\u6587\u6863\u7684\u573a\u666f\uff0c\u5e94\u8be5\u628a\u5b50\u6587\u6863\u62bd\u51fa\u6765\u505a\u6210\u4e00\u4e2a\u65b0\u7684 Collection\uff0c\u7136\u540e\u7528 ref \u548c populate \u6765\u4e0e PostCountry \u5efa\u7acb\u5173\u7cfb\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553073\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ideacco <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @libook \u975e\u5e38\u611f\u8c22\u60a8\u7684\u56de\u590d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553074\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ideacco <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @libook \u53c8\u6363\u9f13\u4e86\u534a\u5929\uff0c\u8fd8\u662f\u4e0d\u884c\u554a\uff0c\u90c1\u95f7\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553075\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \/\/ const body = {<br \/>\/\/ &#8220;country_name&#8221;: &#8220;Canada&#8221;,<br \/>\/\/ &#8220;declared&#8221;: 123,<br \/>\/\/ &#8220;fourPx_send_name&#8221;: &#8220;\u4e2d\u56fd\u90ae 31231 \u653f&#8221;,<br \/>\/\/ &#8220;pid&#8221;: &#8220;5380578836639&#8221;<br \/>\/\/ }<br \/>\/\/ const postCountry = await PostCountry.findOne({ &#8220;pid&#8221;: body.pid });<br \/>\/\/ if (postCountry !== null) {<br \/>\/\/ let isCountryExists = false;<br \/>\/\/ for (const index in postCountry.country_list) {<br \/>\/\/ if (postCountry.country_list[index].fourPx_send_name === body.fourPx_send_name) {<br \/>\/\/ postCountry.country_list[index] = body;<br \/>\/\/ isCountryExists = true;<br \/>\/\/ break; \/\/ \u4e0d\u9700\u8981\u7ee7\u7eed\u5faa\u73af\u4e86<br \/>\/\/ }<br \/>\/\/ }<br \/>\/\/ if (!isCountryExists) {<br \/>\/\/ \/\/ \u5982\u679c\u6ca1\u6709\u627e\u5230\u76f8\u540c\u7684 country \u4fe1\u606f\uff0c\u5c31\u63d2\u5165<br \/>\/\/ postCountry.country_list.push(body);<br \/>\/\/ \/\/ \u5982\u679c country_list \u90e8\u5206\u7684 schema \u7528\u4e86 Mixed\uff0c\u5c31\u5f97\u9700\u8981\u8c03\u7528 markModified\uff0c\u5982\u679c schema \u91cc\u58f0\u660e\u7684\u5c31\u662f\u5bf9\u8c61\u6570\u7ec4\u5c31\u4e0d\u9700\u8981<br \/>\/\/ }<br \/>\/\/ await postCountry.save();<br \/>\/\/ } else {<br \/>\/\/ \/\/ pid \u6ca1\u627e\u5230\u76f8\u5173 document<br \/>\/\/ }<\/p>\n<p>\u5c31\u662f\u67e5\u51fa\u6765\uff0c\u7136\u540e\u5224\u65ad\u6570\u7ec4\u548c\u4fee\u6539\uff0c\u518d save \u8fdb\u53bb\u3002<\/p>\n<p>\u53e6\u5916\u56e0\u4e3a findOne \u548c save \u662f\u4e24\u6b65\u64cd\u4f5c\uff0c\u5982\u679c\u662f\u5206\u5e03\u5f0f\u9ad8\u5e76\u53d1\u7cfb\u7edf\u53ef\u80fd\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u8fd9\u4e2a document \u5c31\u5df2\u7ecf\u88ab\u5176\u4ed6\u5b9e\u4f8b\u4fee\u6539\u8fc7\u4e86\uff0c\u6240\u4ee5\u9700\u8981\u7528\u4e24\u6bb5\u63d0\u4ea4\u6216\u4e8b\u52a1\u6765\u4fdd\u8bc1\u6574\u4e2a\u8fc7\u7a0b\u7684\u539f\u5b50\u6027\uff0cMongoDB \u90fd\u652f\u6301\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553076\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : libook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ee5\u4e3a\u6539\u6210\u6ce8\u91ca\u683c\u5f0f\u5c31\u4e0d\u4f1a\u4e71\u4e86\uff0c\u6ca1\u60f3\u5230 V2 \u76f4\u63a5\u628a\u884c\u5185\u591a\u4e2a\u7a7a\u683c\u5220\u6389\u4e86\uff0c\u4f60\u81ea\u5df1\u8d34\u5230\u7f16\u8f91\u5668\u91cc\uff0c\u53cd\u6ce8\u91ca\u518d\u683c\u5f0f\u5316\u4e00\u4e0b\u770b\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3553077\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ideacco <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @libook \u592a\u611f\u8c22\u4f60\u5566\uff0c\u5144\u5f1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>mongoose \u5982\u4f55\u66f4\u65b0\u5185\u5d4c\u6570\u7ec4&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/163041"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163041"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/163041\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163041"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}