Home > Database > Mysql Tutorial > mongodb高级操作(2)-查询

mongodb高级操作(2)-查询

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 14:49:57
Original
1398 people have browsed it

1.查询文档find介绍 mongodb中使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件.如果不指定条件默认就是{},那么就是查询所有文档. db . test . find () { _id : ObjectId ( 573c858c323f7f2e2ccb0e17 ), name

1.查询文档find介绍

mongodb中使用find来进行查询.find的第一个参数决定了要返回哪些文档,这个参数是一个文档,用于指定查询条件. 如果不指定条件默认就是{},那么就是查询所有文档.
<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>
Copy after login
向查询中指定键值,意味着限定查询条件,查询简单的类型只要指定要查找的值就行了:
<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>
Copy after login
可以在查询中指定多个键值对,以逗号隔开,这样的意思是条件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>
Copy after login

指定返回的键

有时并不是需要返回所有的键,这时可以通过指定find或者findOne的第二个参数来指定想要的键,例如只想查询"name"为"brent"的name和age键: > db.test.find({"name":"brent"},{"name":1,"age":1})
<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>
Copy after login
默认情况下_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>
Copy after login
使用这种方法还可以将_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>
Copy after login

限制

查询传递的参数必须是常量,例如如果想查询一个文档中的某两个列相等的情况是不行的.

2.查询条件

查询除了上面简单介绍的精确匹配,还有更加复杂的查询,比如范围查询,or,and,取反等等

查询条件

"$lt","$lte","$gt","$gte"就是全部的比较操作符.分别对应着<,<=,>和>=,可以将这些组合起来以便查找一个范围的值. 例如下面的例子查找age大于20小于30的文档:
<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>
Copy after login
<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>
Copy after login
对于文档的键值不等于某个特定的值,就要使用"$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>
Copy after login

OR查询

有两种方式进行OR查询:"$in"可以用来查询一个键的多个值,"$or"可以在多个键中查询任意给定的值. 例如下面要查询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)">"$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>
Copy after login
使用"$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>
Copy after login
"$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>
Copy after login
还可以将"$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>
Copy after login
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
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template