title: MongoDB select from array based on multiple conditions
body: <p>I have the following structure (this can't be changed, that is I have to work with):</p>
  
  <pre><code>{
          "_id" : ObjectId("abc123"),
          "notreallyusedfields" : "dontcare",
          "data" : [
                  {
                          "value" : "value1",
                          "otherSomtimesInterestingFields": 1
                          "type" : ObjectId("asd123=type1"),
                  },
                  {
                          "value" : "value2",
                          "otherSometimesInterestingFields": 1
                          "type" : ObjectId("asd1234=type2"),
                  },
                  many others
          ]
  }
  </code></pre>
  
  <p>So basically the fields for a schema are inside an array and they can be identified based on the type field inside 1 array element (1 schema field and it's value is 1 element in the array). For me this is pretty strange, but I'm new to NoSQL so this may be ok. (also for different data some fields may be missing and the element order in the data array is not guaranteed)</p>
  
  <p>Maybe it's easier to understand like this:
  Table a: type1 column | type2 column | and so on (and these are stored in the array like the above)</p>
  
  <p>My question is: how can you select multiple fields with conditions? What I mean is (in SQL): SELECT * FROM table WHERE type1=value1 AND type2=value2</p>
  
  <p>I can select 1 like this:
  <code>db.a.find( {"data.type":ObjectId("asd1234=type2"), "data.value":value2}).pretty()</code></p>
  
  <p>But I don't know how could I include that type1=value1 or even more conditions. And I think this is not even good because it can match any data.value field inside the array so it doesn't mean that where the type is type2 the value has to be value2.</p>
  
  <p>How would you solve this?</p>
  
  <p>I was thinking of doing 1 query for 1 condition and then do another based on the result. So something like the pipeline for aggregation but as I see $match can't be used more times in an aggregation. I guess there is a way to pipeline these commands but this is pretty ugly.
  What am I missing? Or because of the structure of the data I have to do these strange multiple queries?</p>
  
  <p>I've also looked at $filter but the condition there also applies to any element of the array. (Or I'm doing it wrong)</p>
  
  <p>Thanks!</p>
  
  <p>Sorry if I'm not clear enough! Please ask and I can elaborate.</p>
  
  <p>(Basically what I'm trying to do based on this structure ( <a href="https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection">Retrieve only the queried element in an object array in MongoDB collection</a> ) is this: if shape is square then filter for blue colour, if shape is round then filter for red colour === if type is type1 value has to be value1, if type is type2 value has to be value2)</p>