Home > Database > Mysql Tutorial > mongodb高级操作(1)-update

mongodb高级操作(1)-update

WBOY
Release: 2016-06-07 14:49:51
Original
1102 people have browsed it

前面我们已经介绍了mongodb的基本操作:mongodb基本操作下面是介绍一些高级的增删改查操作. 1.更新文档 文档替换 最简单的方法就是文档完全替换,如下就是一个完全替换的例子,先把需要更新的文档找出来并且赋,然后修改所赋的,最后再进行update: a1 = db . test

前面我们已经介绍了mongodb的基本操作:mongodb基本操作 下面是介绍一些高级的增删改查操作.

1.更新文档

文档替换

最简单的方法就是文档完全替换,如下就是一个完全替换的例子,先把需要更新的文档找出来并且赋值,然后修改所赋的值,最后再进行update:
<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>
Copy after login

使用修改器

通常文档只会有一部分需要更新,可以使用原子性的更新修改器对指定文档中的某些键进行更新

"$set"修改器

"$set"修改器用来指定一个字段的值,如果这个字段不存在则创建它,例如先给test表添加一个phone属性:
<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>
Copy after login
然后用修改器再将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>
Copy after login
使用"$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>
Copy after login
使用"$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>
Copy after login

"$inc"增加和减少

"$inc"修改器用来增加和减少已有键的值,只能用于整形,长整形或者双精度浮点型的值,用在其它类型的值上面会报错.对于不存在的键,像"$set"那样也会自动创建相应的键,并且值为给定的值 例如我们用"$inc"修改器给age值+1,原来是30,现在变成了31
<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>
Copy after login
如果要减少,则将age:1里的1改成-1

"$push"数组修改器-添加元素

如果数组已经存在,那么"$push"会向已有的数组末尾添加一个元素,如果没有就创建一个新的数组 ,下面这个例子会创建一个表示comments的数组:
<code class="language-j"></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template