{"id":1088,"date":"2023-03-12T11:23:50","date_gmt":"2023-03-12T03:23:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1088"},"modified":"2023-04-29T11:28:38","modified_gmt":"2023-04-29T03:28:38","slug":"mongodb-document-basic-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-document-basic-operations\/","title":{"rendered":"MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c"},"content":{"rendered":"<h2>MongoDB\u63d2\u5165\u6587\u6863<\/h2>\n<p>\u6587\u6863\u7684\u6570\u636e\u7ed3\u6784\u548cJSON\u57fa\u672c\u4e00\u6837\u3002\u6240\u6709\u5b58\u50a8\u5728\u96c6\u5408\u4e2d\u7684\u6570\u636e\u90fd\u662fBSON\u683c\u5f0f\u3002<\/p>\n<p>BSON\u662f\u4e00\u79cd\u7c7bJSON\u7684\u4e00\u79cd\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u7b80\u79f0Binary JSON\u3002<\/p>\n<p><!-- more --><\/p>\n<h3>\u63d2\u5165\u6587\u6863\u8bed\u6cd5<\/h3>\n<p>MongoDB \u4f7f\u7528 insert() \u6216 save() \u65b9\u6cd5\u5411\u96c6\u5408\u4e2d\u63d2\u5165\u6587\u6863\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.COLLECTION_NAME.insert(document)<\/code><\/pre>\n<h3>\u63d2\u5165\u6587\u6863\u5b9e\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u6587\u6863\u53ef\u4ee5\u5b58\u50a8\u5728 MongoDB \u7684 appblog \u6570\u636e\u5e93 \u7684 site \u96c6\u5408\u4e2d\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.site.insert({title: &#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;MongoDB&#039;, &#039;database&#039;, &#039;NoSQL&#039;],\n    likes: 100\n})<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d site \u662f\u6211\u4eec\u7684\u96c6\u5408\u540d\uff0c\u5982\u679c\u8be5\u96c6\u5408\u4e0d\u5728\u8be5\u6570\u636e\u5e93\u4e2d\uff0cMongoDB \u4f1a\u81ea\u52a8\u521b\u5efa\u8be5\u96c6\u5408\u5e76\u63d2\u5165\u6587\u6863\u3002<\/p>\n<p>\u67e5\u770b\u5df2\u63d2\u5165\u6587\u6863\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.site.find()\n{ &quot;_id&quot; : ObjectId(&quot;5aebc8fd851561202b93df06&quot;), &quot;title&quot; : &quot;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;MongoDB&quot;, &quot;database&quot;, &quot;NoSQL&quot; ], &quot;likes&quot; : 100 }\n> <\/code><\/pre>\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u6570\u636e\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u53d8\u91cf\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-javascript\">> document = ({title: &#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;MongoDB&#039;, &#039;database&#039;, &#039;NoSQL&#039;],\n    likes: 100\n});<\/code><\/pre>\n<p>\u6267\u884c\u540e\u663e\u793a\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">{\n    &quot;title&quot; : &quot;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}<\/code><\/pre>\n<p>\u6267\u884c\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.site.insert(document)\nWriteResult({ &quot;nInserted&quot; : 1 })<\/code><\/pre>\n<blockquote>\n<p>\u63d2\u5165\u6587\u6863\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>db.col.save(document)<\/code> \u547d\u4ee4\u3002\u5982\u679c\u4e0d\u6307\u5b9a _id \u5b57\u6bb5 save() \u65b9\u6cd5\u7c7b\u4f3c\u4e8e insert() \u65b9\u6cd5\u3002\u5982\u679c\u6307\u5b9a _id \u5b57\u6bb5\uff0c\u5219\u4f1a\u66f4\u65b0\u8be5 _id \u7684\u6570\u636e\u3002<\/p>\n<\/blockquote>\n<h3>MongoDB 3.2 \u7248\u672c\u540e\u63d0\u4f9b\u53ef\u7528\u4e8e\u63d2\u5165\u6587\u6863\u7684\u8bed\u6cd5<\/h3>\n<p><code>db.collection.insertOne()<\/code>\uff1a\u5411\u6307\u5b9a\u96c6\u5408\u4e2d\u63d2\u5165\u4e00\u6761\u6587\u6863\u6570\u636e<br \/>\n<code>db.collection.insertMany()<\/code>\uff1a\u5411\u6307\u5b9a\u96c6\u5408\u4e2d\u63d2\u5165\u591a\u6761\u6587\u6863\u6570\u636e<\/p>\n<ul>\n<li>\u63d2\u5165\u5355\u6761\u6570\u636e<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">> var document = db.collection.insertOne({&quot;a&quot;: 3})\n> document\n{\n    &quot;acknowledged&quot; : true,\n    &quot;insertedId&quot; : ObjectId(&quot;5aebcafb851561202b93df08&quot;)\n}<\/code><\/pre>\n<ul>\n<li>\u63d2\u5165\u591a\u6761\u6570\u636e<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">> var res = db.collection.insertMany([{&quot;b&quot;: 1}, {&#039;c&#039;: 2}])\n> res\n{\n    &quot;acknowledged&quot; : true,\n    &quot;insertedIds&quot; : [\n        ObjectId(&quot;5aebcb6c851561202b93df09&quot;),\n        ObjectId(&quot;5aebcb6c851561202b93df0a&quot;)\n    ]\n}<\/code><\/pre>\n<h2>MongoDB\u66f4\u65b0\u6587\u6863<\/h2>\n<p>MongoDB \u4f7f\u7528 update() \u548c save() \u65b9\u6cd5\u6765\u66f4\u65b0\u96c6\u5408\u4e2d\u7684\u6587\u6863\u3002<\/p>\n<h3>update() \u65b9\u6cd5<\/h3>\n<h4>\u8bed\u6cd5<\/h4>\n<p>update() \u65b9\u6cd5\u7528\u4e8e\u66f4\u65b0\u5df2\u5b58\u5728\u7684\u6587\u6863\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.collection.update(\n   &lt;query&gt;,\n   &lt;update&gt;,\n   {\n     upsert: &lt;boolean&gt;,\n     multi: &lt;boolean&gt;,\n     writeConcern: &lt;document&gt;\n   }\n)<\/code><\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li><code>query<\/code>\uff1aupdate\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u7c7b\u4f3csql update\u67e5\u8be2\u5185where\u540e\u9762\u7684\u3002<\/li>\n<li><code>update<\/code>\uff1aupdate\u7684\u5bf9\u8c61\u548c\u4e00\u4e9b\u66f4\u65b0\u7684\u64cd\u4f5c\u7b26\uff08\u5982$,$inc&#8230;\uff09\u7b49\uff0c\u4e5f\u53ef\u4ee5\u7406\u89e3\u4e3asql update\u67e5\u8be2\u5185set\u540e\u9762\u7684\u3002<\/li>\n<li><code>upsert<\/code>\uff1a\u53ef\u9009\uff0c\u8fd9\u4e2a\u53c2\u6570\u7684\u610f\u601d\u662f\uff0c\u5982\u679c\u4e0d\u5b58\u5728update\u7684\u8bb0\u5f55\uff0c\u662f\u5426\u63d2\u5165objNew\u3002true\u4e3a\u63d2\u5165\uff0c\u9ed8\u8ba4\u662ffalse\uff0c\u4e0d\u63d2\u5165\u3002<\/li>\n<li><code>multi<\/code>\uff1a\u53ef\u9009\uff0cMongoDB \u9ed8\u8ba4\u662ffalse\uff0c\u53ea\u66f4\u65b0\u627e\u5230\u7684\u7b2c\u4e00\u6761\u8bb0\u5f55\uff0c\u5982\u679c\u8fd9\u4e2a\u53c2\u6570\u4e3atrue\uff0c\u5c31\u628a\u6309\u6761\u4ef6\u67e5\u51fa\u6765\u591a\u6761\u8bb0\u5f55\u5168\u90e8\u66f4\u65b0\u3002<\/li>\n<li><code>writeConcern<\/code>\uff1a\u53ef\u9009\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\u3002<\/li>\n<li>\n<ul>\n<li>WriteConcern.NONE\uff1a\u6ca1\u6709\u5f02\u5e38\u629b\u51fa<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.NORMAL\uff1a\u4ec5\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\uff0c\u6ca1\u6709\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.SAFE\uff1a\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\u3001\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38\uff1b\u5e76\u7b49\u5f85\u670d\u52a1\u5668\u5b8c\u6210\u5199\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.MAJORITY\uff1a\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\u3001\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38\uff1b\u5e76\u7b49\u5f85\u4e00\u4e2a\u4e3b\u670d\u52a1\u5668\u5b8c\u6210\u5199\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.FSYNC_SAFE\uff1a\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\u3001\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38\uff1b\u5199\u64cd\u4f5c\u7b49\u5f85\u670d\u52a1\u5668\u5c06\u6570\u636e\u5237\u65b0\u5230\u78c1\u76d8<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.JOURNAL_SAFE\uff1a\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\u3001\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38\uff1b\u5199\u64cd\u4f5c\u7b49\u5f85\u670d\u52a1\u5668\u63d0\u4ea4\u5230\u78c1\u76d8\u7684\u65e5\u5fd7\u6587\u4ef6<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>WriteConcern.REPLICAS_SAFE\uff1a\u629b\u51fa\u7f51\u7edc\u9519\u8bef\u5f02\u5e38\u3001\u670d\u52a1\u5668\u9519\u8bef\u5f02\u5e38\uff1b\u7b49\u5f85\u81f3\u5c112\u53f0\u670d\u52a1\u5668\u5b8c\u6210\u5199\u64cd\u4f5c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u6211\u4eec\u5728\u96c6\u5408 col \u4e2d\u63d2\u5165\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.insert({title: &#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;mongodb&#039;, &#039;database&#039;, &#039;NoSQL&#039;],\n    likes: 100\n})<\/code><\/pre>\n<p>\u63a5\u7740\u6211\u4eec\u901a\u8fc7 update() \u65b9\u6cd5\u6765\u66f4\u65b0\u6807\u9898(title)\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.update({&#039;title&#039;:&#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;}, {$set:{&#039;title&#039;:&#039;MongoDB\u6587\u6863\u66f4\u65b0\u64cd\u4f5c&#039;}})\nWriteResult({ &quot;nMatched&quot; : 1, &quot;nUpserted&quot; : 0, &quot;nModified&quot; : 1 })\n> db.col.find().pretty()\n{\n    &quot;_id&quot; : ObjectId(&quot;5aebcfa8851561202b93df0c&quot;),\n    &quot;title&quot; : &quot;MongoDB\u6587\u6863\u66f4\u65b0\u64cd\u4f5c&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;mongodb&quot;,\n        &quot;database&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 100\n}\n> <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u6807\u9898(title)\u7531\u539f\u6765\u7684 &quot;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&quot; \u66f4\u65b0\u4e3a &quot;MongoDB\u6587\u6863\u66f4\u65b0\u64cd\u4f5c&quot;\u3002<\/p>\n<p>\u4ee5\u4e0a\u8bed\u53e5\u53ea\u4f1a\u4fee\u6539\u7b2c\u4e00\u6761\u53d1\u73b0\u7684\u6587\u6863\uff0c\u5982\u679c\u9700\u8981\u4fee\u6539\u591a\u6761\u76f8\u540c\u7684\u6587\u6863\uff0c\u5219\u9700\u8981\u8bbe\u7f6e multi \u53c2\u6570\u4e3a true\u3002<\/p>\n<pre><code class=\"language-javascript\">> db.col.update({&#039;title&#039;:&#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;}, {$set:{&#039;title&#039;:&#039;MongoDB\u6587\u6863\u66f4\u65b0\u64cd\u4f5c&#039;}}, {multi:true})<\/code><\/pre>\n<h3>save() \u65b9\u6cd5<\/h3>\n<h4>\u8bed\u6cd5<\/h4>\n<p>save() \u65b9\u6cd5\u901a\u8fc7\u4f20\u5165\u7684\u6587\u6863\u6765\u66ff\u6362\u5df2\u6709\u6587\u6863\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.collection.save(\n   &lt;document&gt;,\n   {\n     writeConcern: &lt;document&gt;\n   }\n)<\/code><\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>document\uff1a\u6587\u6863\u6570\u636e\u3002<\/li>\n<li>writeConcern\uff1a\u53ef\u9009\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u6211\u4eec\u66ff\u6362\u4e86 _id \u4e3a 5aebcfa8851561202b93df0c \u7684\u6587\u6863\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.save({\n    &quot;_id&quot; : ObjectId(&quot;5aebcfa8851561202b93df0c&quot;),\n    &quot;title&quot; : &quot;MongoDB&quot;,\n    &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n    &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n    &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n    &quot;tags&quot; : [\n        &quot;MongoDB&quot;,\n        &quot;NoSQL&quot;\n    ],\n    &quot;likes&quot; : 110\n})<\/code><\/pre>\n<p>\u66ff\u6362\u6210\u529f\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 find() \u547d\u4ee4\u6765\u67e5\u770b\u66ff\u6362\u540e\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">> db.col.find().pretty()\n{\n        &quot;_id&quot; : ObjectId(&quot;56064f89ade2f21f36b03136&quot;),\n        &quot;title&quot; : &quot;MongoDB&quot;,\n        &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;,\n        &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;,\n        &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;,\n        &quot;tags&quot; : [\n            &quot;MongoDB&quot;,\n            &quot;NoSQL&quot;\n        ],\n        &quot;likes&quot; : 110\n}<\/code><\/pre>\n<h4>\u66f4\u591a\u5b9e\u4f8b<\/h4>\n<p>\u53ea\u66f4\u65b0\u7b2c\u4e00\u6761\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $set : { &quot;test&quot; : &quot;OK&quot;} } );<\/code><\/pre>\n<p>\u5168\u90e8\u66f4\u65b0\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $set : { &quot;test&quot; : &quot;OK&quot;} }, false, true );<\/code><\/pre>\n<p>\u53ea\u6dfb\u52a0\u7b2c\u4e00\u6761\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $set : { &quot;test&quot; : &quot;OK&quot;} }, true, false );<\/code><\/pre>\n<p>\u5168\u90e8\u6dfb\u52a0\u8fdb\u53bb\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $set : { &quot;test&quot; : &quot;OK&quot;} }, true, true );<\/code><\/pre>\n<p>\u5168\u90e8\u66f4\u65b0\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $inc : { &quot;count&quot; : 1} }, false, true );<\/code><\/pre>\n<p>\u53ea\u66f4\u65b0\u7b2c\u4e00\u6761\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.col.update( { &quot;count&quot; : { $gt : 8 } } , { $inc : { &quot;count&quot; : 1} }, false, false );<\/code><\/pre>\n<h3>MongoDB 3.2\u7248\u672c\u5f00\u59cb\uff0cMongoDB\u63d0\u4f9b\u66f4\u65b0\u96c6\u5408\u6587\u6863\u7684\u65b9\u6cd5<\/h3>\n<ul>\n<li><code>db.collection.updateOne()<\/code>\uff1a\u5411\u6307\u5b9a\u96c6\u5408\u66f4\u65b0\u5355\u4e2a\u6587\u6863<\/li>\n<li><code>db.collection.updateMany()<\/code>\uff1a\u5411\u6307\u5b9a\u96c6\u5408\u66f4\u65b0\u591a\u4e2a\u6587\u6863<\/li>\n<\/ul>\n<p>\u9996\u5148\u6211\u4eec\u5728test\u96c6\u5408\u91cc\u63d2\u5165\u6d4b\u8bd5\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">use test\ndb.test_collection.insert([\n  {&quot;name&quot;:&quot;china&quot;,&quot;age&quot;:&quot;20&quot;,&quot;status&quot;:&quot;a&quot;},\n  {&quot;name&quot;:&quot;america&quot;,&quot;age&quot;:&quot;19&quot;,&quot;status&quot;:&quot;b&quot;},\n  {&quot;name&quot;:&quot;russia&quot;,&quot;age&quot;:&quot;18&quot;,&quot;status&quot;:&quot;c&quot;},\n])<\/code><\/pre>\n<p>\u66f4\u65b0\u5355\u4e2a\u6587\u6863<\/p>\n<pre><code class=\"language-javascript\">> db.test_collection.updateOne({&quot;name&quot;:&quot;china&quot;},{$set:{&quot;age&quot;:&quot;28&quot;}})\n{ &quot;acknowledged&quot; : true, &quot;matchedCount&quot; : 1, &quot;modifiedCount&quot; : 1 }\n> db.test_collection.find()\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716af&quot;), &quot;name&quot; : &quot;china&quot;, &quot;age&quot; : &quot;28&quot;, &quot;status&quot; : &quot;a&quot; }\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716b0&quot;), &quot;name&quot; : &quot;america&quot;, &quot;age&quot; : &quot;19&quot;, &quot;status&quot; : &quot;b&quot; }\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716b1&quot;), &quot;name&quot; : &quot;russia&quot;, &quot;age&quot; : &quot;18&quot;, &quot;status&quot; : &quot;c&quot; }\n><\/code><\/pre>\n<p>\u66f4\u65b0\u591a\u4e2a\u6587\u6863<\/p>\n<pre><code class=\"language-javascript\">> db.test_collection.updateMany({&quot;age&quot;:{$gt:&quot;10&quot;}},{$set:{&quot;status&quot;:&quot;xyz&quot;}})\n{ &quot;acknowledged&quot; : true, &quot;matchedCount&quot; : 3, &quot;modifiedCount&quot; : 3 }\n> db.test_collection.find()\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716af&quot;), &quot;name&quot; : &quot;china&quot;, &quot;age&quot; : &quot;28&quot;, &quot;status&quot; : &quot;xyz&quot; }\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716b0&quot;), &quot;name&quot; : &quot;america&quot;, &quot;age&quot; : &quot;19&quot;, &quot;status&quot; : &quot;xyz&quot; }\n{ &quot;_id&quot; : ObjectId(&quot;59c8ba673b92ae498a5716b1&quot;), &quot;name&quot; : &quot;russia&quot;, &quot;age&quot; : &quot;18&quot;, &quot;status&quot; : &quot;xyz&quot; }\n><\/code><\/pre>\n<h2>MongoDB\u5220\u9664\u6587\u6863<\/h2>\n<h3>remove() \u65b9\u6cd5<\/h3>\n<p>MongoDB remove()\u51fd\u6570\u662f\u7528\u6765\u79fb\u9664\u96c6\u5408\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>MongoDB\u6570\u636e\u66f4\u65b0\u53ef\u4ee5\u4f7f\u7528update()\u51fd\u6570\u3002\u5728\u6267\u884cremove()\u51fd\u6570\u524d\u5148\u6267\u884cfind()\u547d\u4ee4\u6765\u5224\u65ad\u6267\u884c\u7684\u6761\u4ef6\u662f\u5426\u6b63\u786e\uff0c\u8fd9\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u4e60\u60ef\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<p>remove() \u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>db.collection.remove(\n   &lt;query&gt;,\n   &lt;justOne&gt;\n)<\/code><\/pre>\n<p>\u5982\u679c MongoDB \u662f 2.6 \u7248\u672c\u4ee5\u540e\u7684\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>db.collection.remove(\n   &lt;query&gt;,\n   {\n     justOne: &lt;boolean&gt;,\n     writeConcern: &lt;document&gt;\n   }\n)<\/code><\/pre>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>query\uff1a\uff08\u53ef\u9009\uff09\u5220\u9664\u7684\u6587\u6863\u7684\u6761\u4ef6\u3002<\/li>\n<li>justOne\uff1a\uff08\u53ef\u9009\uff09\u5982\u679c\u8bbe\u4e3a true \u6216 1\uff0c\u5219\u53ea\u5220\u9664\u4e00\u4e2a\u6587\u6863\u3002<\/li>\n<li>writeConcern\uff1a\uff08\u53ef\u9009\uff09\u629b\u51fa\u5f02\u5e38\u7684\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u4ee5\u4e0b\u6587\u6863\u6211\u4eec\u6267\u884c\u4e24\u6b21\u63d2\u5165\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.insert({title: &#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;, \n    description: &#039;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&#039;,\n    by: &#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;,\n    url: &#039;http:\/\/www.appblog.cn&#039;,\n    tags: [&#039;mongodb&#039;, &#039;database&#039;, &#039;NoSQL&#039;],\n    likes: 100\n})<\/code><\/pre>\n<p>\u4f7f\u7528 find() \u51fd\u6570\u67e5\u8be2\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.find()\n{ &quot;_id&quot; : ObjectId(&quot;5aebc8fd851561202b93df06&quot;), &quot;title&quot; : &quot;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;mongodb&quot;, &quot;database&quot;, &quot;NoSQL&quot; ], &quot;likes&quot; : 100 }\n{ &quot;_id&quot; : ObjectId(&quot;5aebca2c851561202b93df07&quot;), &quot;title&quot; : &quot;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&quot;, &quot;description&quot; : &quot;MongoDB \u662f\u4e00\u4e2a Nosql \u6570\u636e\u5e93&quot;, &quot;by&quot; : &quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;, &quot;url&quot; : &quot;http:\/\/www.appblog.cn&quot;, &quot;tags&quot; : [ &quot;mongodb&quot;, &quot;database&quot;, &quot;NoSQL&quot; ], &quot;likes&quot; : 100 }\n> <\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u79fb\u9664 title \u4e3a &#8216;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#8217; \u7684\u6587\u6863\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.remove({&#039;title&#039;:&#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;})\nWriteResult({ &quot;nRemoved&quot; : 2 })           # \u5220\u9664\u4e86\u4e24\u6761\u6570\u636e\n> db.col.find()\n\u2026\u2026                                        # \u6ca1\u6709\u6570\u636e<\/code><\/pre>\n<p>\u5982\u679c\u53ea\u60f3\u5220\u9664\u7b2c\u4e00\u6761\u627e\u5230\u7684\u8bb0\u5f55\u53ef\u4ee5\u8bbe\u7f6e justOne \u4e3a 1\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.remove({&#039;title&#039;:&#039;MongoDB\u6587\u6863\u57fa\u672c\u64cd\u4f5c&#039;}, {justOne:1})<\/code><\/pre>\n<p>\u5982\u679c\u60f3\u5220\u9664\u6240\u6709\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\uff08\u7c7b\u4f3c\u5e38\u89c4 SQL \u7684 truncate \u547d\u4ee4\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.col.remove({})<\/code><\/pre>\n<h3>deleteOne() \u548c deleteMany() \u65b9\u6cd5<\/h3>\n<p>remove() \u65b9\u6cd5\u5df2\u7ecf\u8fc7\u65f6\uff0c\u73b0\u5728\u5b98\u65b9\u63a8\u8350\u4f7f\u7528 deleteOne() \u548c deleteMany() \u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u5220\u9664\u96c6\u5408\u4e0b\u5168\u90e8\u6587\u6863\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.inventory.deleteMany({})\n\n\u5220\u9664 status \u7b49\u4e8e A \u7684\u5168\u90e8\u6587\u6863\uff1a\n\n```javascript\ndb.inventory.deleteMany({ status : \"A\" })<\/code><\/pre>\n<p>\u5220\u9664 status \u7b49\u4e8e D \u7684\u4e00\u4e2a\u6587\u6863\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.inventory.deleteOne( { status: \"D\" } )<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB\u63d2\u5165\u6587\u6863 \u6587\u6863\u7684\u6570\u636e\u7ed3\u6784\u548cJSON\u57fa\u672c\u4e00\u6837\u3002\u6240\u6709\u5b58\u50a8\u5728\u96c6\u5408\u4e2d\u7684\u6570\u636e\u90fd\u662fBSON\u683c\u5f0f\u3002 BSON\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}