title: mongodb aggregate division between to arrays last elements
body: <p>I have a feed with products that I store in a db (so Im not in control over the structure of the data or its properties). The feed is stored in a mongoDB and accessible later on a website. This is example of the data stored in db.</p>
  <pre><code>  {
      productName: 'ABC',
      price: {
        previous: [null, null, 100],
        latest: [200, 200, 200],
      },
    };
  </code></pre>
  <p>On the site I want to list products in order of largest discount. I.e price.latest / price.previous. I've tried alot and I want to do something like this:</p>
  <pre><code>         $set: {
                discount: {
                  $cond: {
                    if: {
                      $and: [
                        { $gt: [{ $last: '$price.latest' }, null] },
                        { $gt: [{ $last: '$price.previous' }, null] },
                      ],
                    },
                    then: {
                      $divide: [
                        { $last: '$price.latest' },
                        { $last: '$price.previous' },
                      ],
                    },
                    else: null,
                  },
                },
            }
  </code></pre>
  <p>I cant get it to work and I dont know if its the null check of division crashes. :/</p>