MongoDB权威指南(3)- 查询
jopen
13年前
<div id="article_content" class="article_content"> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><span style="line-height:22px;font-size:15px;"><strong>1.find方法介绍</strong></span></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">在不传入参数的情况下,find方法缺省使用{}做参数,它匹配所有的document。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">我们可以传入一个查询document给find方法来限制输出,如:查找年龄为27的用户</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">27</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">一个查询document里可以包括多个条件,如:查询年龄为27并且名字叫joe的用户</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">27</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">条件之间都是And关系。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em><span style="line-height:22px;font-family:宋体;font-size:15px;">指定查询返回的内容</span></em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">有时候我们并不需要返回document里所有的key/value对,我们可以给find(或findOne)方法传入第二个参数来指定返回哪些key的值。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">比如:我们只想要用户的名字和Email</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">})<br /> {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523620</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">),<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">email</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe@example.com</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">"_id"键总是会被返回。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果想指定不想返回的key,如:返回除了fatal_weakness以外的key的值</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fatal_weakness</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">0</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">用这个方法可以把缺省返回的"_id"给排除掉</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({}, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">0</span> <span style="line-height:1.5;">})<br /> {<br /> </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>限制</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查询document里边key的值对数据库来说必须是个常量,假如collection里边有两个key,一个叫int_stock一个叫num_sold,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果我们想查询这两个key相等的document,下边的查询是行不通的</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.stock.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">in_stock</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">this.num_sold</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}) </span> <span style="line-height:1.5;color:#008000;">//</span> <span style="line-height:1.5;color:#008000;"> doesn't work</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>2.查询条件</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">"$lt", "$lte", "$gt", 和"$gte"分别对应<,<=,>,>=</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如:查询年龄在18到30之间的用户</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$gte</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">18</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$lte</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">30</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">“$ne" 不等于,可以用于任意类型的数据</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如,查找名字不叫joe的用户</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">username</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$ne</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>OR查询</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">mongoDB里有两种使用OR的方法,如果是一个key对应的多个值,可以用"$in",还有个”or"是一种更通用的方法。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如:我们在进行一个抽奖活动,ticket No是725,542,390的三个中奖了,我们可以这样把这三个document查出来</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.raffle.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">ticket_no</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$in</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">725</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">542</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">390</span> <span style="line-height:1.5;">]}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">"$in"里边指定的值可以是不同的数据类型</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">与"$in"相反的操作就是"$nin”,返回指定的key的值不存在于数组里边的document,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.raffle.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">ticket_no</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$nin</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">725</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">542</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">390</span> <span style="line-height:1.5;">]}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">上边的查询返回没中奖的所有ticket。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果要在多个key的查询条件之间建立Or关系就得用"$or",如,查询彩票号码为<span style="line-height:19px;">725</span><span style="line-height:19px;">, </span><span style="line-height:19px;">542</span><span style="line-height:19px;">, </span><span style="line-height:19px;">390</span><span style="line-height:19px;"> </span> 或者winner为true的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.raffle.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$or</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [{</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">ticket_no</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$in</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">725</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">542</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">390</span> <span style="line-height:1.5;">]}},{</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">winner</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">}]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>$not</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">"$not"可以应用于上边说的所有的条件操作符上,举个例子之前我们先看下求余(取模)操作符"$mod","$mod"指定一个整数数组做为key的值,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">里边两个数字,第一个是对其求余的数字,第二个是余数。比如,我们要找id_num的值对5求余,余数为1的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">id_num</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$mod</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">5</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">]}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">现在我们使用$not,如果找id_num对5求余后余数不为1的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">id_num</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$not</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$mod</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">5</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">]}}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>规律</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">回头再看下前一章的更新修饰符,就会发现同样是$打头的符号,它和本章说的查询条件符号的位置是不一样的,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">在查询里,"$lt"属于内层document,而更新的时候"$inc"是外层document的key。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">一个key上可以有多个查询条件,却不能有多个更新。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查询年龄为20到30的用户可以这样做</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$lt</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">30</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$gt</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">20</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">但是却没有下边这种更新修饰符</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">{</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$inc</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$set</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {age : </span> <span style="line-height:1.5;">40</span> <span style="line-height:1.5;">}}</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>3.特殊类型查询</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>null</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">null同时匹配自身和不存在的key,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find()<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523621</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;"> }<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523622</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;"> }<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f148d22aa494fd523623</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">2</span> <span style="line-height:1.5;"> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果要查找y为null的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;">})<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523621</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;"> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查找key为null的document时,缺少此key的所有document也会返回</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">z</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;">})<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523621</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;"> }<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f0dfd22aa494fd523622</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;"> }<br /> { </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : ObjectId(</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">4ba0f148d22aa494fd523623</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">), </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">y</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">2</span> <span style="line-height:1.5;"> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果想查找key存在并且为null的document,就要用"$exists"符号,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">z</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$in</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;color:#0000ff;">null</span> <span style="line-height:1.5;">], </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$exists</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个办法看起来比较笨,没办法,我们没有"$eq"条件符号。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>正则表达式</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查找名字叫joe,但是忽略大小写的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.users.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">/</span> <span style="line-height:1.5;">joe</span> <span style="line-height:1.5;">/</span> <span style="line-height:1.5;">i})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">mongoDB使用Perl Compatible Regular Expression (PCRE)来匹配正则表达式,所有PCRE允许的语法都可以在mongoDB里使用。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>查询数组</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查询数组很简单,通常情况下,数组的每个元素的值都能看作key的值。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">例如,我们有一些水果</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">我们查找水果里有香蕉的ducoment</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">用起来就好像我们有这么一个document在</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">{</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">当然,上边这个document是不合法的。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$all</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果要匹配数组里的多个元素,就要用“$all”。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">例如,我们有这样一个collection</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">2</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">kumquat</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">orange</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">cherry</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">要查询既有apple又有banana的document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({fruit : {$all : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]}})<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]}<br /> {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">_id</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">cherry</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]}</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$all的值里边的元素是没有顺序的,只要目标包含里边的每个元素即可,如果不使用$all,就会执行精确的匹配检查。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">上边这个查询会匹配第一个document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个查询就不会匹配第一个document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">]})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">同样,这个查询也不会匹配第一个document</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果要匹配数组里指定的元素,就要使用key.index这种语法</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit.2</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">index 是从0开始索引的</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$size</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">按照数组中元素的个数查询</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.food.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">fruit</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$size</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$size 不能和比较条件符号(如$gt)联合使用</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$slice操作符</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">上边曾经提到过,使用find方法的第二个参数,指定哪些键值会被返回,$slice操作符可以返回数组元素的一个子集。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">例如,查找博客和它的前10条评论</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne(criteria, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$slice</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">或者,如果想要后10条评论的话</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne(criteria, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$slice</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">也可以返回中间的一段结果</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.blog.posts.findOne(criteria, {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">comments</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$slice</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : [</span> <span style="line-height:1.5;">23</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">]}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">上边的查询跳过前23个元素,返回第24个到第34个元素。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>查询嵌入的document</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">查询嵌入的整个document和普通的查询没有差别,比如我们有这样一个document</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">{<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">first</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">,<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">last</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Schmoe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"><br /> },<br /> </span> <span style="line-height:1.5;"> "</span> <span style="line-height:1.5;">age</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">45</span> <span style="line-height:1.5;"><br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">那么我们就可以这样查询名字叫Joe Schmoe的人</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.people.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : {</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">first</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">last</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Schmoe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">}})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果Joe要加一个middle name,这个查询就不行了,这种查询必须匹配整个嵌入的document,而且key是有顺序的。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">我们可以用.来直接查询嵌入的key。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.people.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name.first</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Joe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">name.last</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">Schmoe</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">点号在查询用document里就被解释为“通向嵌入的document”,所以document的key里边不能包含点号。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>$where 查询</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">$where子句允许你在查询里执行arbitary javascript,使你几乎可以在查询中做任何事情。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">最常见的例子就是比较document里边两个key的值。举个例子,我们有个list,我们想返回里边的key的值里有相等的document</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">(随便哪两个key,只要它们的value相等即可)。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">banana</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">6</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">peach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.insert({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">apple</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">8</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">spinach</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">4</span> <span style="line-height:1.5;">, </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">watermelon</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">4</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">第二个document里,菠菜和西瓜的值是相等的,这个应该返回,这个使用$条件查询符号是做不到的。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$where</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;color:#0000ff;">function</span> <span style="line-height:1.5;"> () {<br /> ... </span> <span style="line-height:1.5;color:#0000ff;">for</span> <span style="line-height:1.5;"> (</span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> current </span> <span style="line-height:1.5;color:#0000ff;">in</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">this</span> <span style="line-height:1.5;">) {<br /> ... </span> <span style="line-height:1.5;color:#0000ff;"> for</span> <span style="line-height:1.5;"> (</span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> other </span> <span style="line-height:1.5;color:#0000ff;">in</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">this</span> <span style="line-height:1.5;">) {<br /> ... </span> <span style="line-height:1.5;color:#0000ff;"> if</span> <span style="line-height:1.5;"> (current </span> <span style="line-height:1.5;">!=</span> <span style="line-height:1.5;"> other </span> <span style="line-height:1.5;">&&</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">this</span> <span style="line-height:1.5;">[current] </span> <span style="line-height:1.5;">==</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">this</span> <span style="line-height:1.5;">[other]) {<br /> ... </span> <span style="line-height:1.5;color:#0000ff;"> return</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">true</span> <span style="line-height:1.5;">;<br /> ... }<br /> ... }<br /> ... }<br /> ... </span> <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">false</span> <span style="line-height:1.5;">;<br /> ... }});</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如果函数返回true,那么这个document就会作为结果集的一部分被返回。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">刚才我们定义了一个函数,给$where查询指定一个字符串是一样的效果</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$where</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">this.x + this.y == 10</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.foo.find({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">$where</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">function() { return this.x + this.y == 10; }</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这两个查询是等价的。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如非必要,尽量不要使用$where查询,$where查询比一般的查询慢很多,每个document都必须从BSON转换为一个javascript对象,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">然后执行$where表达式。而且,索引也不能使用。你可以通过组合使用$where查询和非$where查询来降低使用它的代价。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><strong>4.游标</strong></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">find方法使用游标返回查询结果,游标的客户端实现使你可以对最终结果做很多的控制。在shell里创建一个游标很简单,往collection里放些document,</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">执行查询,将返回结果指派给一个本地变量即可。</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">for</span> <span style="line-height:1.5;">(i</span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;">0</span> <span style="line-height:1.5;">; i</span> <span style="line-height:1.5;"><</span> <span style="line-height:1.5;">100</span> <span style="line-height:1.5;">; i</span> <span style="line-height:1.5;">++</span> <span style="line-height:1.5;">) {<br /> ... db.c.insert({x : i});<br /> ... }<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.collection.find();</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">你可以使用next方法来遍历结果,使用hasNext方法来检查有没有下一个,典型的循环如下</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">while</span> <span style="line-height:1.5;"> (cursor.hasNext()) {<br /> ... obj </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> cursor.next();<br /> ... </span> <span style="line-height:1.5;color:#008000;">//</span> <span style="line-height:1.5;color:#008000;"> do stuff</span> <span style="line-height:1.5;color:#008000;"><br /> </span> <span style="line-height:1.5;">... }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">cursor类同样实现了iterator接口,所以你可以使用forEach循环</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.people.find();<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> cursor.forEach(</span> <span style="line-height:1.5;color:#0000ff;">function</span> <span style="line-height:1.5;">(x) {<br /> ... print(x.name);<br /> ... });<br /> adam<br /> matt<br /> zak</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">当你调用find方法的时候,shell并不会立刻去查询数据库,直到你真正请求结果的时候才发送查询,这样你可以在实际执行查询之前</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">追加一些其他的选项,游标的这些方法几乎都是返回游标本身,所以你可以按任意顺序链入这些方法,下边三个查询是等价的</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.foo.find().sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">x</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}).limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">).skip(</span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">);<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.foo.find().limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">).sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">x</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">}).skip(</span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">);<br /> </span> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> </span> <span style="line-height:1.5;color:#0000ff;">var</span> <span style="line-height:1.5;"> cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.foo.find().skip(</span> <span style="line-height:1.5;">10</span> <span style="line-height:1.5;">).limit(</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">).sort({</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;">x</span> <span style="line-height:1.5;">"</span> <span style="line-height:1.5;"> : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">});</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">在这个时候查询并未执行,所有的函数都只是构建查询。现在我们调用hasNext方法,</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> cursor.hasNext()</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这时候查询被送到服务器,shell就立刻取到了前100条结果或者前4MB的结果,所以接下来在调用next方法的时候就不会再发送请求</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">以及接受结果,当第一次返回的结果集用完的时候,shell会再次联系服务器,请求更多的结果。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>Limits, Skips, 和Sorts</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">limit函数限制返回的结果集的上限,如,只返回3个结果</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find().limit(</span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">)</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">skip函数跳过前x个结果,返回剩余的</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find().skip(</span> <span style="line-height:1.5;">3</span> <span style="line-height:1.5;">)</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">sort方法使用一组键值对做参数,key是document里的key的名字,value是1升序或者-1降序。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">如,按名字升序和年龄降序排序</p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">></span> <span style="line-height:1.5;"> db.c.find().sort({username : </span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">, age : </span> <span style="line-height:1.5;">-</span> <span style="line-height:1.5;">1</span> <span style="line-height:1.5;">})</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>比较顺序</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">mongoDB有一个关于各种数据类型之间比较的等级制度。在某些情况下,你可能有一个key,它的值有多种类型,如果你想按照该</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">key排序,mongoDB有一个预定义好的顺序,它们从小到大分别为:</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><span style="line-height:18px;font-size:12px;"> 1. Minimum value</span><br /> <span style="line-height:18px;font-size:12px;"> 2. null</span><br /> <span style="line-height:18px;font-size:12px;"> 3. Numbers (integers, longs, doubles)</span><br /> <span style="line-height:18px;font-size:12px;"> 4. Strings</span><br /> <span style="line-height:18px;font-size:12px;"> 5. Object/document</span><br /> <span style="line-height:18px;font-size:12px;"> 6. Array</span><br /> <span style="line-height:18px;font-size:12px;"> 7. Binary data</span><br /> <span style="line-height:18px;font-size:12px;"> 8. Object ID</span><br /> <span style="line-height:18px;font-size:12px;"> 9. Boolean</span><br /> <span style="line-height:18px;font-size:12px;"> 10. Date</span><br /> <span style="line-height:18px;font-size:12px;"> 11. Timestamp</span><br /> <span style="line-height:18px;font-size:12px;"> 12. Regular expression</span><br /> <span style="line-height:18px;font-size:12px;"> 13. Maximum value</span></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><em>获取一致性的结果</em></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">处理数据的常见方式就是从mongoDB里取出来,然后修改它,再存储进mongoDB,如下:</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><span style="line-height:18px;font-size:12px;"> </span></p> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;line-height:20px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code"> <div> <span style="line-height:1.5;">cursor </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> db.foo.find();<br /> <br /> </span> <span style="line-height:1.5;color:#0000ff;">while</span> <span style="line-height:1.5;"> (cursor.hasNext()) {<br /> </span> <span style="line-height:1.5;color:#0000ff;"> var</span> <span style="line-height:1.5;"> doc </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> cursor.next();<br /> doc </span> <span style="line-height:1.5;">=</span> <span style="line-height:1.5;"> process(doc);<br /> db.foo.save(doc);<br /> }</span> </div> </div> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">结果集数量很小的时候,这样子做是没问题的,如果结果集很大,这个做法就行不通了。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">想象一下document是如何存储的,你可以像下图4-1一样来理解存储document的collection,每个雪花代表一个document,因为</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">它们和document一样,都很漂亮而且独一无二。</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="MongoDB权威指南(3)- Querying " src="https://simg.open-open.com/show/ab90a5471aeaa71ce87f687876dc9231.bmp" width="545" height="159" /></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">图4-1</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">现在,我们执行一个find,它从头开始向右返回结果。程序获取前100个document并处理它们,然后将它们存储回数据库,如果一个</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">document没有足够的空间来容纳新的document,如图4-2,那么就需要给它重新指定存储位置。通常,这个新的document 会重新</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">定位到collection的尾部,如图4-3.</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="MongoDB权威指南(3)- Querying " src="https://simg.open-open.com/show/b9597edf74627367d0a332be7abb60fa.bmp" width="545" height="283" /></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">图4-2</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="MongoDB权威指南(3)- Querying " src="https://simg.open-open.com/show/fa79a98110405ee076d110f3144051c2.bmp" width="709" height="159" /></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">图4-3</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">现在,我们的程序持续不断地批量获取document,当它到达collect尾部的时候,那些被重新定位的document会再次返回,如图4-4.</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;"><img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="MongoDB权威指南(3)- Querying " src="https://simg.open-open.com/show/e6dec9c335b9adaf050c0dd890cd07e4.bmp" width="710" height="219" /></p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">图4-4</p> <p style="line-height:20px;margin:5px auto;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;font-size:13px;">这个问题的解决办法是,对查询使用snapshot,如果添加了“$snapshot"选项,查询就会按照collection未被改变的视图运行。</p> </div>