Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use either the $and or the $all operator to do what I believe that you are looking to accomplish. </p> <p>Given your example document:</p> <pre><code>&gt; db.test.find().pretty() { "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] } </code></pre> <p>You can use the $and operator to search for a document whose "keywords" array contains both words. </p> <pre><code>&gt; db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]}) { "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] } &gt; </code></pre> <p>The $all operator will return the same result, and the query is a little more streamlined:</p> <pre><code>&gt; db.test.find({keywords:{$all:["apollo", "landings"]}}) { "_id" : ObjectId("4f26b716c27b085280a45a29"), "name" : "Apollo", "text" : "Some text about Apollo moon landings", "keywords" : [ "some", "text", "about", "apollo", "moon", "landings" ] } </code></pre> <p>If we put an index on the keywords array, both queries make use of it. </p> <pre><code>&gt; db.test.ensureIndex({keywords:1}) &gt; db.test.find({$and:[{keywords:"apollo"}, {keywords:"landings"}]}).explain() { "cursor" : "BtreeCursor keywords_1", "nscanned" : 1, "nscannedObjects" : 1, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : true, "indexOnly" : false, "indexBounds" : { "keywords" : [ [ "apollo", "apollo" ] ] } } &gt; db.test.find({keywords:{$all:["apollo", "landings"]}}).explain() { "cursor" : "BtreeCursor keywords_1", "nscanned" : 1, "nscannedObjects" : 1, "n" : 1, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : true, "indexOnly" : false, "indexBounds" : { "keywords" : [ [ "apollo", "apollo" ] ] } } &gt; </code></pre> <p>Both queries make use of the keywords index. </p> <p>For more information on the different types of queries, please refer to the "Advanced Queries" document.<br> <a href="http://www.mongodb.org/display/DOCS/Advanced+Queries" rel="nofollow">http://www.mongodb.org/display/DOCS/Advanced+Queries</a></p> <p>For more information on how indexing works in Mongo, please refer to the "Indexing" document.<br> <a href="http://www.mongodb.org/display/DOCS/Indexes#Indexes-IndexingArrayElements" rel="nofollow">http://www.mongodb.org/display/DOCS/Indexes#Indexes-IndexingArrayElements</a></p> <p>The "Indexing Array Elements" section links to the documentation on MultiKeys. <a href="http://www.mongodb.org/display/DOCS/Multikeys" rel="nofollow">http://www.mongodb.org/display/DOCS/Multikeys</a></p> <p>If you are unfamiliar with the .explain function of mongodb, it is explained here: <a href="http://www.mongodb.org/display/DOCS/Explain" rel="nofollow">http://www.mongodb.org/display/DOCS/Explain</a> In a nutshell, it displays any indexes that your query is using, and how many documents needed to be accessed in order to return the relevant ones. </p> <p>Finally, your question seems similar to that of another user who was asking about searching for values in arrays earlier this morning. Perhaps this will be relevant to you as well.<br> <a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/38f30a56094d9e3e" rel="nofollow">http://groups.google.com/group/mongodb-user/browse_thread/thread/38f30a56094d9e3e</a></p> <p>Hopefully, this will help you to write the query that you are looking for. Please let us know if you have any follow-up questions!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload