title: Problem using aggregation in mongodb retrieving data from two collections
body: <p>i am strugling with a query that i don't know how to perform... I have two collections,</p>
  
  <p><strong>Tarifas Collection</strong></p>
  
  <pre><code>tarifaConfig = new Schema({
      producto: { type: String },
      titulo: { type: String },
      bloqueo: { type: Boolean },
      margen: { type: Number },
      precioVenta: { type: Number },
      precioVentaIva: { type: Number },
  })
  
  const tarifaSchema = new Schema({
      codigo: { type: String },
      titulo: { type: String },
      margen: { type: Number },
      estado: { type: Boolean },
      bloqueo: { type: Boolean },
      configs: [tarifaConfig]
  })
  </code></pre>
  
  <p><strong>Producto Collection</strong></p>
  
  <pre><code>const productosSchema = new Schema({
      ref: { type: String },
      nombre: { type: String },
      precioCompra: { type: Number },
      precioCompraIva: { type: Number },
      precioVenta: { type: Number },
      precioVentaIva: { type: Number },
      iva: { type: Number },
  })
  </code></pre>
  
  <p>Now i am using an Aggregation method to retrieve both collection in a response</p>
  
  <pre><code> productosModel.aggregate([
          {
              $match: { _id: ObjectId(req.params.id) }
          },
          {
              $lookup: {
                  from: "tarifas",
                  as: "tarifas",
                  pipeline: []
              }
          }
      ]).then((producto) =&gt; {
          res.json(producto);
      })
  </code></pre>
  
  <p>This is working and gives me both collections in the response... but..
  In tarifa's collection i have a propertie called 'configs' that is an array with lot of sub collections... this sub collections are a config of each product that i have,
  So what i need to do is, retrieve all tarifas that has a configs for the product, and if the configs does not contain retrieve the tarifa with a empty array.</p>
  
  <p><strong>Expected result</strong></p>
  
  <pre><code>{
     ref: 'rbe34',
     nombre: 'bike',
     precioCompra: 10,
     precioCompraIva: 12.1,
     precioVenta: "",
     precioVentaIva: "",
     iva: 21,
     tarifas:[
     { 
      codigo: 'NOR',
      titulo: 'Normal tarifa',
      margen: 33,
      estado: true,
      bloqueo: true,
      configs: [], ///HERE I NEED A EMPTY ARRAY IF THERE IS NOT ANY CONFIG THAT MATCH WITH THE PRODUCT ID,  
     }
     ]
  }
  </code></pre>
  
  <p>i tried to add $match in my aggregation pipeline.</p>
  
  <pre><code>   productosModel.aggregate([
          {
              $match: { _id: ObjectId(req.params.id) }
          },
          {
              $lookup: {
                  from: "tarifas",
                  as: "tarifas",
                  pipeline: [
                      { $match: { 'configs.producto': req.params.id } }
                  ]
              }
          }
      ])
  </code></pre>
  
  <p>But if there is not any config that match the product it doesn't retrieve the rest of Tarifa's collection</p>