MongoDB的查询操作

zsho5453 3年前
   <p style="text-align:center"><img src="https://simg.open-open.com/show/0a1afb447f41fd826e78491eb98c9289.png"></p>    <h2><strong>1. 前言</strong></h2>    <p>在这篇文章中,我们将学习如何查询mongoDB中的数据。当我们把数据存储在mongoDB以后,我们需要把数据查询出来。毕竟CRUD操作中,查询操作在我们系统中是我们应用比较频繁的操作。我们需要应对不同的业务需求,构造合适的查询条件去查询我们想要的数据。我们需要去学习mongoDB给我们提供了哪些查询相关的语法和功能。在这里,我们使用mongodb自带的mongo shell( mongo shell是一个javascript环境的mongodb客户端,支持js语法 )来学习。</p>    <h2><strong>2. 准备</strong></h2>    <p>在开始之前,我们需要准备一下实验用的数据:</p>    <pre>  <code class="language-sql">  // 启动mongo shell客户端  $ mongo    // 在这里我们使用test数据库,如果没有这个数据库,会自动创建  > use test    // 在users collection中插入6条用户数据  > db.users.insertMany(    [       {         _id: 1,         name: "sue",         age: 19,         type: 1,         status: "P",         favorites: { artist: "Picasso", food: "pizza" },         finished: [ 17, 3 ],         badges: [ "blue", "black" ],         points: [            { points: 85, bonus: 20 },            { points: 85, bonus: 10 }         ]       },       {         _id: 2,         name: "bob",         age: 42,         type: 1,         status: "A",         favorites: { artist: "Miro", food: "meringue" },         finished: [ 11, 25 ],         badges: [ "green" ],         points: [            { points: 85, bonus: 20 },            { points: 64, bonus: 12 }         ]       },       {         _id: 3,         name: "ahn",         age: 22,         type: 2,         status: "A",         favorites: { artist: "Cassatt", food: "cake" },         finished: [ 6 ],         badges: [ "blue", "red" ],         points: [            { points: 81, bonus: 8 },            { points: 55, bonus: 20 }         ]       },       {         _id: 4,         name: "xi",         age: 34,         type: 2,         status: "D",         favorites: { artist: "Chagall", food: "chocolate" },         finished: [ 5, 11 ],         badges: [ "red", "black" ],         points: [            { points: 53, bonus: 15 },            { points: 51, bonus: 15 }         ]       },       {         _id: 5,         name: "xyz",         age: 23,         type: 2,         status: "D",         favorites: { artist: "Noguchi", food: "nougat" },         finished: [ 14, 6 ],         badges: [ "orange" ],         points: [            { points: 71, bonus: 20 }         ]       },       {         _id: 6,         name: "abc",         age: 43,         type: 1,         status: "A",         favorites: { food: "pizza", artist: "Picasso" },         finished: [ 18, 12 ],         badges: [ "black", "blue" ],         points: [            { points: 78, bonus: 8 },            { points: 57, bonus: 7 }         ]       }    ]  )    View Code</code></pre>    <h2><strong>3. 基本查询</strong></h2>    <p>MongoDB提供了 <strong>db.collection.find()</strong> 方法来执行查询操作。find方法接受两个参数:一个查询条件,一个是投影的字段。这两个参数都不是必须的,如果省略了查询条件,则默认列出collection中的所有文档。</p>    <pre>  <code class="language-sql">db.users.find()    // 这个和上面的语句是等价的  db.users.find({})  </code></pre>    <h3><strong>3.1 等值查询</strong></h3>    <p>通过find()方法来执行等值查询的时候,可以通过 <strong> { <em><field>:<value>}</em> </strong> 的方式来指定查询条件,这个条件表示在collection中查询满足field的值为value的所有文档。假设我们需要查找所有status是'A'的用户,我们可以这么查询:</p>    <pre>  <code class="language-sql">db.users.find({status: 'A'})  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <h3><strong>3.2 操作符查询</strong></h3>    <p>mongodb支持使用操作符来构造查询条件,比如比较操作符,如$gt,$lt等。使用操作符的查询条件通过 <em> <strong>{<field>: {<operator>:<value>}}</strong> </em> 来表示。假设我们要查询age超过22的用户,可以这么做:</p>    <pre>  <code class="language-sql">db.users.find({age: {$gt: 22}})  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "favorites" : { "artist" : "Miro", "food" : "meringue" }, "finished" : [ 11, 25 ], "badges" : [ "green" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }  { "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  { "_id" : 5, "name" : "xyz", "age" : 23, "type" : 2, "status" : "D", "favorites" : { "artist" : "Noguchi", "food" : "nougat" }, "finished" : [ 14, 6 ], "badges" : [ "orange" ], "points" : [ { "points" : 71, "bonus" : 20 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <h3><strong>3.3 and关系</strong></h3>    <p>mongodb支持的and关系查询很简单,当一个查询条件中包含多个<field:value>对的时候,这些条件之间的关系就是and关系。所以and关系的查询可以这样表示 <em> {<field1>:<value1>, <field2>:<value2>, ...,<fieldN>:<valueN>} </em> 。假设我们要查询满足条件:status是'D',并且age大于23的用户,我们可以这么查询:</p>    <pre>  <code class="language-sql">db.users.find({status: 'D', age: {$gt: 23}})  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql"> "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  </code></pre>    <h3><strong>3.4 or关系</strong></h3>    <p>mongodb通过操作符"$or"来支持or关系的查询。or关系的查询条件可以这么构造: <em> {$or: [{<field1>:<value1>}, {<field2>:<value2>},..., {<fieldN>:<valueN>}]} </em> 。比如我们想查询status是'A' 或者age大于23的用户,我们可以这么做:</p>    <pre>  <code class="language-sql">db.users.find({$or: [{status: 'A'}, {age: {$gt: 23}}]})  </code></pre>    <p>查询条件</p>    <pre>  <code class="language-sql">{ "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "favorites" : { "artist" : "Miro", "food" : "meringue" }, "finished" : [ 11, 25 ], "badges" : [ "green" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }  { "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  { "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <h2><strong>4. 嵌套文档查询</strong></h2>    <p>如果在文档中的一个字段上的值也是一个文档,当我们需要查询这个文档中的子文档中的值得时候,就涉及到嵌套文档的查询。MongoDB支持对嵌套文档的查询。</p>    <h3><strong>4.1 匹配整个子文档</strong></h3>    <p>匹配整个子文档可以认为也是一种等值查询,只不过这次的值是一个子文档。所以查询条件就是这样的 <em> <strong>{<field>:<sub document>}</strong> </em> 。假设我们要查询favorites这个字段上,artist为'Picasso',food为'pizza'的用户,我们可以这样做:</p>    <pre>  <code class="language-sql">db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  </code></pre>    <h3><strong>4.2 匹配子文档中的字段</strong></h3>    <p>mongodb支持匹配子文档中的字段,通过点(.)符号来表示子文档中的字段,比如我们在favorites字段对应的子文档中的artist字段上做查询操作,可以通过 <em> <strong>favorites.artist</strong> </em> 来表示这个子文档中的artist字段</p>    <pre>  <code class="language-sql">db.users.find( { "favorites.artist": "Picasso" } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <h2><strong>5. 在数组上查询</strong></h2>    <p>如果文档中的字段的值是数组类型,mongodb支持对数组类型的字段构造查询条件。</p>    <h3><strong>5.1 匹配整个数组</strong></h3>    <p>mongodb对整个数组进行匹配的时候,和匹配整个嵌套文档的方式类似。只要在条件中给出整个数组就可以了,就像这样 <em> <strong>{<field>:<value>}</strong> </em> ,只不过这里的<value>是需要匹配的整个数组。假设我们需要查找badges的值为"['blue', 'black']"的用户,我们可以这样做</p>    <pre>  <code class="language-sql">db.users.find( { badges: [ "blue", "black" ] } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  </code></pre>    <h3><strong>5.2 查找数组中的一个元素</strong></h3>    <p>除了对数组类型的字段做完全匹配的查询,我们也可以把数组的元素作为查询条件,主要数组中包含了这个元素,那么都会被匹配到。比如我们需要查找badges的数组中包含了'black'元素的所有用户,我们可以这样做</p>    <pre>  <code class="language-sql">db.users.find( { badges: "black" } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  { "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <p>可以看到,这三个用户的badges字段中的值中,都有‘black’元素在数组中。</p>    <h3><strong>5.3 匹配数组中指定下标的元素</strong></h3>    <p>mongodb支持把数组中指定下标的元素作为查询条件来构造查询语句,通过类似于引用子文档的点号(.)方式来说引用指定下标的元素。比如badges字段中数组的第一个元素,可以表示成: <strong>badges.0</strong> ,类似的,第N个就是 <strong>badges.N</strong> 。假设我们想查找满足badges数组中的第一个元素是'black'的用户,我们可以这样做:</p>    <pre>  <code class="language-sql">db.users.find( { "badges.0": "black" } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <p>查询结果看到,只有一个满足条件的用户。虽然别的用户的badges字段也有'black'字段,但是由于需要查找数组的第一个元素是'black'的用户,所以只有一个用户是满足条件的。</p>    <h3><strong>5.4 对数组元素指定多个查询条件</strong></h3>    <h3><strong>多个条件之间的and关系</strong></h3>    <p>mongodb提供了一个"$elemMatch"操作符,这个操作符的作用是对数组中的元素进行多条件匹配,只要数组中至少一个元素满足指定的条件,那么就表示匹配成功,也就是说,'$elemMatch'操作符指定的条件之间是"与"的关系。来看例子,假设我们要找到满足finished字段中的数组元素的值大于15,小于20,我们可以这样做:</p>    <pre>  <code class="language-sql">db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )  </code></pre>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <p>我们可以看到,“$elemMatch”操作符的用法,是用指定的多个条件来匹配数组中的每一个值,只要数组中至少有一个值满足我们指定的条件,就表示匹配成功。</p>    <h3><strong>多条件之间的or关系</strong></h3>    <p>mongodb在数组匹配的时候,可以返回满足指定条件的结果的并集。通俗的讲,指定的条件之间是“或”的关系,即只要数组中的任何一个元素满足多个查询条件中的任何一个,那么就认为这个文档被匹配上了。比如,我们如果这样指定查询条件</p>    <pre>  <code class="language-sql">db.users.find( { finished: { $gt: 15, $lt: 20 } } )  </code></pre>    <p>我们先来看下查询的结果</p>    <pre>  <code class="language-sql">{ "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "status" : "P", "favorites" : { "artist" : "Picasso", "food" : "pizza" }, "finished" : [ 17, 3 ], "badges" : [ "blue", "black" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 85, "bonus" : 10 } ] }  { "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "favorites" : { "artist" : "Miro", "food" : "meringue" }, "finished" : [ 11, 25 ], "badges" : [ "green" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <p>这个查询条件和上面的元素匹配的查询条件相比,查询的结果我们看到,中间的记录中,finished的值是[11, 25]的也满足查询条件。这是因为 <strong>{finished: {$gt: 15, $lt: 20}}</strong> 这个条件,表示数组中的任何一个元素,只要满足大于15或者小于20,都被认为是满足查询条件的,指定的条件之间是“或”的关系。而通过"$elemMatch"操作符指定的查询条件,条件之间的关系是“与”的关系。所以不难理解,值为[11, 25]的那个记录之所以被匹配,是因为它满足25是大于条件中的15的,而11是小于条件中的20的,所以自然就满足条件了。</p>    <h3><strong>5.5 数组中包含子文档的查询</strong></h3>    <p>当数组中包含子文档的时候,也可以为这些子文档中的字段构造查询条件。</p>    <h3><strong>使用数组下标定位到具体的子文档</strong></h3>    <p>这种方式的查询条件,是同时利用数组的下标表示和文档中字段的点号(.)表示法来指定数组中子文档中的字段。讲的有点绕,我们来看具体的例子。</p>    <pre>  <code class="language-sql">db.users.find( { 'points.0.points': { $lte: 55 } } )  </code></pre>    <p>这条查询语句的意思,表示我们要查询points字段中,它包含的数组中下标为0的子文档中的points字段的值,满足条件{$lte: 55},也就是这个子文档中的points字段的值小于等于55。查询结果如下:</p>    <pre>  <code class="language-sql">{"_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  </code></pre>    <h3><strong>匹配任何一个满足条件的子文档</strong></h3>    <p>如果我们省略了数组的下标,那么查询条件就变成了这样</p>    <pre>  <code class="language-sql">db.users.find( { 'points.points': { $lte: 55 } } )  </code></pre>    <p>它表示查询points数组中任何一个子文档,只要子文档中的points字段的值满足条件{$lte: 55}就可以了。</p>    <p>查询结果:</p>    <pre>  <code class="language-sql">{ "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  { "_id" : 4, "name" : "xi", "age" : 34, "type" : 2, "status" : "D", "favorites" : { "artist" : "Chagall", "food" : "chocolate" }, "finished" : [ 5, 11 ], "badges" : [ "red", "black" ], "points" : [ { "points" : 53, "bonus" : 15 }, { "points" : 51, "bonus" : 15 } ] }  </code></pre>    <p>可以看到,查询结果中包含了所有满足条件的记录。</p>    <h3><strong>多个条件之间的and关系</strong></h3>    <p>利用上面提到的数组的多条件“and”关系匹配的方法,也可以用来为数组中的子文档指定多个匹配条件。查询条件也是利用了"$elemMatch"操作符:</p>    <pre>  <code class="language-sql">db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )  </code></pre>    <p>可以看到,我们在对points数组中的子文档指定多个匹配匹配条件的时候,和上面提到的对数组中元素指定多个匹配条件的方式类似。只不过,上面是对整数指定匹配条件,这里是换成了对子文档指定匹配条件,而且子文档中的字段可以直接引用,不用采用点(.)号的方式引用。</p>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  </code></pre>    <h3><strong>多个条件之间的or关系</strong></h3>    <p>和对数组中元素指定多个or关系的查询条件一样,对数组中的子文档指定多个or关系的查询条件的语句,在形式上类似。来看具体的例子:</p>    <pre>  <code class="language-sql">db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )  </code></pre>    <p>这里使用点(.)号来引用数组中的子文档的字段,其中两个条件之间是“或”的关系,语句的意思大体是这样的:查询满足points数组中,任何一个子文档的points字段的值不小于70或者任何一个子文档的bonus字段的值为20的记录。查询结果是这样的:</p>    <pre>  <code class="language-sql">{ "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "favorites" : { "artist" : "Miro", "food" : "meringue" }, "finished" : [ 11, 25 ], "badges" : [ "green" ], "points" : [ { "points" : 85, "bonus" : 20 }, { "points" : 64, "bonus" : 12 } ] }  { "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  </code></pre>    <p>如果我们对数组的下标做了指定,那么可以在上面的多个“or”条件之上,再加一个数组元素的位置限定。比如:</p>    <pre>  <code class="language-sql">db.users.find({'points.0.points': {$gte: 70}, 'points.0.bonus': 8})  </code></pre>    <p>这里使用了上面提到的,在数组中指定下标的方式来指定数组中的某一个元素。</p>    <p>查询结果</p>    <pre>  <code class="language-sql">{ "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "favorites" : { "artist" : "Cassatt", "food" : "cake" }, "finished" : [ 6 ], "badges" : [ "blue", "red" ], "points" : [ { "points" : 81, "bonus" : 8 }, { "points" : 55, "bonus" : 20 } ] }  { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "favorites" : { "food" : "pizza", "artist" : "Picasso" }, "finished" : [ 18, 12 ], "badges" : [ "black", "blue" ], "points" : [ { "points" : 78, "bonus" : 8 }, { "points" : 57, "bonus" : 7 } ] }  </code></pre>    <h2><strong>6. 总结</strong></h2>    <p>到这里,我们已经差不多讲完了mongo中有关查询语句的语法。从对简单值的查询,到对嵌套子文档的查询,再到数组的查询,最后到数组和子文档嵌套的复杂查询。以及如何对多个查询条件做“or”操作和“and”操作。希望这篇文章对各位有一些帮助吧。</p>    <p>讲了那么多,光看没用,还是要多动手实践,自己动手去敲一遍代码才会加深印象,如果能在项目中的业务场景中需要用到这些查询,那么就再好不过了。因为这样才会让这些知识更好地被吸收,不然就会掉入学了忘,忘了学的深渊之中。这其实也是写博客的好处,学了新的知识,即使不能马上用到工作中,通过一篇博客来巩固加深印象,虽然没在实际项目中使用产生的效果好,也是也是有一定的效果的。</p>    <p> </p>    <p>来自:http://www.cnblogs.com/duke2016/p/5992521.html</p>    <p> </p>