ホームページ >データベース >mysql チュートリアル >mongodb高级操作(1)-update
前面我们已经介绍了mongodb的基本操作:mongodb基本操作下面是介绍一些高级的增删改查操作. 1.更新文档 文档替换 最简单的方法就是文档完全替换,如下就是一个完全替换的例子,先把需要更新的文档找出来并且赋,然后修改所赋的,最后再进行update: a1 = db . test
前面我们已经介绍了mongodb的基本操作:mongodb基本操作 下面是介绍一些高级的增删改查操作.<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> a1</span><span class="pun" style="color:rgb(147,161,161)">=</span><span class="pln" style="color:rgb(72,72,76)">db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">findOne</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"5738785d132e1e47e535a177"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"x"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">3.14</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"y"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">5.5</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> a1</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"5738785d132e1e47e535a177"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"x"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">3.14</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"y"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">5.5</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="kwd" style="color:rgb(30,52,123)">delete</span><span class="pln" style="color:rgb(72,72,76)"> a1</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">x</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="kwd" style="color:rgb(30,52,123)">true</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> a1</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"5738785d132e1e47e535a177"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"y"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">5.5</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> a1</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">content</span><span class="pun" style="color:rgb(147,161,161)">={</span><span class="pln" style="color:rgb(72,72,76)">name</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">'job'</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)">age</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">11</span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"job"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">11</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> a1</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"5738785d132e1e47e535a177"</span><span class="pun" style="color:rgb(147,161,161)">),</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"y"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">5.5</span><span class="pun" style="color:rgb(147,161,161)">,</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"content"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">{</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"job"</span><span class="pun" style="color:rgb(147,161,161)">,</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">11</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">update</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"5738785d132e1e47e535a177"</span><span class="pun" style="color:rgb(147,161,161)">)},</span><span class="pln" style="color:rgb(72,72,76)">a1</span><span class="pun" style="color:rgb(147,161,161)">)</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></code>
<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> </span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> <strong>db</strong></span><strong><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">update</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">)},{</span><span class="str" style="color:rgb(221,17,68)">"$set"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="pln" style="color:rgb(72,72,76)">phone</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">666</span><span class="pun" style="color:rgb(147,161,161)">}})</span></strong></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><strong><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></strong></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><strong><span class="pun" style="color:rgb(147,161,161)">></span></strong></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">666</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>然后用修改器再将phone该为888
<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><strong><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">update</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">)},{</span><span class="str" style="color:rgb(221,17,68)">"$set"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="pln" style="color:rgb(72,72,76)">phone</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">888</span><span class="pun" style="color:rgb(147,161,161)">}})</span></strong></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">888</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>使用"$set"修改器还可以修改字段的属性,例如可以把phone改成一个数组,还可以用"$unset"将键完全删除:
<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"> </code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">update</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">)},{</span><span class="str" style="color:rgb(221,17,68)">"$unset"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="pln" style="color:rgb(72,72,76)">phone</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">888</span><span class="pun" style="color:rgb(147,161,161)">}})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>使用"$set"修改内嵌文档:
<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"57395fa85f74882a9bfa2d9f"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">888</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"address"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"abc"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> <strong>db</strong></span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">.</span><span class="pln" style="color:rgb(72,72,76); font-weight:bold">test</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">.</span><span class="pln" style="color:rgb(72,72,76); font-weight:bold">update</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">({</span><span class="str" style="color:rgb(221,17,68); font-weight:bold">"_id"</span><span class="pln" style="color:rgb(72,72,76); font-weight:bold"> </span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">:</span><span class="pln" style="color:rgb(72,72,76); font-weight:bold"> </span><span class="typ" style="color:teal; font-weight:bold">ObjectId</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">(</span><span class="str" style="color:rgb(221,17,68); font-weight:bold">"57395fa85f74882a9bfa2d9f"</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">)},{</span><span class="str" style="color:rgb(221,17,68); font-weight:bold">"$set"</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">:{</span><span class="str" style="color:rgb(221,17,68)"><strong><span style="color:#ff00ff">"id.phone"</span></strong></span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">:</span><span class="lit" style="color:rgb(25,95,145); font-weight:bold">666</span><span class="pun" style="color:rgb(147,161,161); font-weight:bold">}})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><strong><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></strong></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">30</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"57395fa85f74882a9bfa2d9f"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">666</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"address"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"abc"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>
<code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)"><span style="color:#ff00ff"><strong>30</strong></span></span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"57395fa85f74882a9bfa2d9f"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">666</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"address"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"abc"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">update</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">)},{</span><span class="str" style="color:rgb(221,17,68)">"$inc"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="pln" style="color:rgb(72,72,76)">age</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">}})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="typ" style="color:teal">WriteResult</span><span class="pun" style="color:rgb(147,161,161)">({</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nMatched"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nUpserted"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"nModified"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">1</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">})</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">></span><span class="pln" style="color:rgb(72,72,76)"> db</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">test</span><span class="pun" style="color:rgb(147,161,161)">.</span><span class="pln" style="color:rgb(72,72,76)">find</span><span class="pun" style="color:rgb(147,161,161)">()</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"573957c55f74882a9bfa2d9e"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)"><span style="color:#ff00ff"><strong>31</strong></span></span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"email"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"xxxx@qq.com"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code><code class="language-js" style="margin:8px 0px; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:break-word"><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="typ" style="color:teal">ObjectId</span><span class="pun" style="color:rgb(147,161,161)">(</span><span class="str" style="color:rgb(221,17,68)">"57395fa85f74882a9bfa2d9f"</span><span class="pun" style="color:rgb(147,161,161)">),</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"id"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">{</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"phone"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="lit" style="color:rgb(25,95,145)">666</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"address"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="str" style="color:rgb(221,17,68)">"abc"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>如果要减少,则将age:1里的1改成-1
<code class="language-j"></code>