1.查询文档find介绍 mongodb中使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.如果不指定条件默认就是{},那么就是查询所有文档. db . test . find () { _id : ObjectId ( 573c858c323f7f2e2ccb0e17 ), name
<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)">"573c858c323f7f2e2ccb0e17"</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)">43</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)">"status"</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)">"done"</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)">"573c86d3017c5eb7d08aed6d"</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)">"bob"</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)">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)">"status"</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)">"done"</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)">"573c88fe017c5eb7d08aed6e"</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)">"tom"</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)">10</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)">"status"</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)">"done"</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)">"573c8bd3323f7f2e2ccb0e18"</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)">"status"</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)">"done"</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><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"brent"</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)">"573c858c323f7f2e2ccb0e17"</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)">43</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)">"status"</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)">"done"</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)">"573c8bd3323f7f2e2ccb0e18"</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)">"status"</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)">"done"</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>可以在查询中指定多个键值对,以逗号隔开,这样的意思是条件1 AND 条件2 AND ....的意思:
<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><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">43</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)">"573c858c323f7f2e2ccb0e17"</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)">43</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)">"status"</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)">"done"</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)">"573c858c323f7f2e2ccb0e17"</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)">43</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)">"573c8bd3323f7f2e2ccb0e18"</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="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>默认情况下_id都是会返回的,也可以使用第二个参数来剔除某个键,例如我们不希望得到status的键:
<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><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">},{</span><span class="str" style="color:rgb(221,17,68)">"<strong>status</strong>"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)"><strong>0</strong></span><span class="pun" style="color:rgb(147,161,161)">})</span>
<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)">"573c858c323f7f2e2ccb0e17"</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)">43</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)">"573c8bd3323f7f2e2ccb0e18"</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="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>
使用这种方法还可以将_id列剔除:
<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><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"brent"</span><span class="pun" style="color:rgb(147,161,161)">},{</span><span class="str" style="color:rgb(221,17,68)">"status"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">0</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="str" style="color:rgb(221,17,68)">"_id"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">0</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)">"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)">43</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)">"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="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)">test2</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)">"573e72449e178b5475b29d89"</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)">28</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)">"573e73149e178b5475b29d8a"</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)">10</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)">"573e73ae9e178b5475b29d8b"</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)">"bob"</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)">14</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)">test2</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><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="str" style="color:rgb(221,17,68)">"$gt"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">20</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="str" style="color:rgb(221,17,68)">"$lt"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">30</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)">"573e72449e178b5475b29d89"</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)">28</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>对于文档的键值不等于某个特定的值,就要使用"$ne"了,他表示不相等.下面例子要查找name不等于brent的用户:
<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)">test2</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><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="str" style="color:rgb(221,17,68)">"$ne"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"brent"</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)">"573e73ae9e178b5475b29d8b"</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)">"bob"</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)">14</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)">test2</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><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="str" style="color:rgb(221,17,68)">"$in"</span><span class="pun" style="color:rgb(147,161,161)">:[</span><span class="lit" style="color:rgb(25,95,145)">10</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="lit" style="color:rgb(25,95,145)">14</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)">"573e73149e178b5475b29d8a"</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)">10</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)">"573e73ae9e178b5475b29d8b"</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)">"bob"</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)">14</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>使用"$in"的时候,可以指定不同类型的条件和值,如果"$in"的数组只有一个值,那么和直接匹配是一样的."$in"和"nin"是相对的,"nin"返回数组中不匹配的文档.查询age不为10,14的文档:
<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)">test2</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><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="str" style="color:rgb(221,17,68)">"$nin"</span><span class="pun" style="color:rgb(147,161,161)">:[</span><span class="lit" style="color:rgb(25,95,145)">10</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="lit" style="color:rgb(25,95,145)">14</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)">"573e72449e178b5475b29d89"</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)">28</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>"$in"只能对单个键做OR查询,如果想对多个键做匹配OR查询那么可以使用"$or","$or"接受一个包含所有可能的数组作为条件. 例如下面这个查询name为bob或者age为10的文档:
<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)">test2</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><span class="str" style="color:rgb(221,17,68)">"$or"</span><span class="pun" style="color:rgb(147,161,161)">:[{</span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"bob"</span><span class="pun" style="color:rgb(147,161,161)">},{</span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="lit" style="color:rgb(25,95,145)">10</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)">"573e73149e178b5475b29d8a"</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)">10</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)">"573e73ae9e178b5475b29d8b"</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)">"bob"</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)">14</span><span class="pln" style="color:rgb(72,72,76)"> </span><span class="pun" style="color:rgb(147,161,161)">}</span></code>还可以将"$or"和"in"联合起来使用:
<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)">test2</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><span class="str" style="color:rgb(221,17,68)">"$or"</span><span class="pun" style="color:rgb(147,161,161)">:[{</span><span class="str" style="color:rgb(221,17,68)">"age"</span><span class="pun" style="color:rgb(147,161,161)">:{</span><span class="str" style="color:rgb(221,17,68)">"$in"</span><span class="pun" style="color:rgb(147,161,161)">:[</span><span class="lit" style="color:rgb(25,95,145)">10</span><span class="pun" style="color:rgb(147,161,161)">,</span><span class="lit" style="color:rgb(25,95,145)">28</span><span class="pun" style="color:rgb(147,161,161)">]}},{</span><span class="str" style="color:rgb(221,17,68)">"name"</span><span class="pun" style="color:rgb(147,161,161)">:</span><span class="str" style="color:rgb(221,17,68)">"bob"</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)">"573e72449e178b5475b29d89"</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)">28</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)">"573e73149e178b5475b29d8a"</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)">10</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)">"573e73ae9e178b5475b29d8b"</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)">"bob"</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"></span></code>