title: Using $size and $addToSet to compare multiple arrays from the same cluster
body: <p>Here is an example of the kind of documents I'm querying:</p>
  
  <p><strong>INPUT:</strong></p>
  
  <pre><code>}
      "_id": ObjectId("2786872873872"),
      "data_shop" : {
          "records_data" : [
              {
                  "artist_name" : [
                      {
                          "val" : "BEYONCE",
                      },
                  ],
                  "album_name" : [
                      {
                          "val" : "COUNTDOWN",
                      }
                  ],
                  "qty" : [
                      0,
                      1,
                      2,
                      3
                  ]
              },
              {
                  "artist_name" : [
                      {
                          "val" : "MUSE",
                      },
                  ],
                  "album_name" : [
                      {
                          "val" : "THE RESISTANCE",
                      }
                  ],
                  "qty" : [
                      0,
                      1,
                      2,
                      3,
                      3
                  ]
              }
          ],
      },
  }
  }
      "_id": ObjectId("2786872855555"),
      "data_shop" : {
          "records_data" : [
              {
                  "artist_name" : [
                      {
                          "val" : "MAC MILLER",
                      },
                  ],
                  "album_name" : [
                      {
                          "val" : "SWIMMING",
                      }
                  ],
                  "qty" : [
                      0,
                      1,
                      2,
                      3,
                  ]
              },
              {
                  "artist_name" : [
                      {
                          "val" : "DAFT PUNK",
                      },
                  ],
                  "album_name" : [
                      {
                          "val" : "RANDOM ACCESS MEMORIES",
                      }
                  ],
                  "qty" : [
                      0,
                      1,
                      2,
                      3,
                      4,
                  ]
              }
          ],
      },
  }
  </code></pre>
  
  <p><strong>What I've done so far:</strong> </p>
  
  <p>I'm trying to use both $size and $addtoSet in order to return the ObjectIds that have repeated numbers in the <strong>qty</strong> field. As you can see, only the first ObjectId has a repeated number (3) in the qty field.
  This is what I've done so far:</p>
  
  <pre><code>db.mycollection.aggregate(
      [
          {$match: {"data_shop.records_data.qty.1": {$lte: 1}}},
          {
              $project: {Album_Cluster:"$data_shop.records_data"}
          },
          {
              $unwind: "$Album_Cluster"
          },
          {
              $project: {qty: "$Album_Cluster.qty"},
          },
          {
              $project: {qty_size: {$size: "$qty"}, qty:1}
          },
          { $match: {"qty_size.1": {$exists: false}, qty_size: {$gt: 1} }},
          {$group:
              {_id: "$_id",
              totalSize: {$push: "$qty_size"},
              realSize: {$addToSet: "$qty"},
              }
          },
      ],
      {allowDiskUse: true}
  )
  </code></pre>
  
  <p>And this is the result of the query above, in order to check the functionality of the query:</p>
  
  <pre><code>{"_id":ObjectId("2786872873872"), "totalSize": [4, 5], "realSize":[[0, 1, 2, 3]]}
  {"_id":ObjectId("2786872855555"), "totalSize": [4, 5], "realSize":[[0, 1, 2, 3], [0, 1, 2, 3, 4]]}
  </code></pre>
  
  <p>I'm a little bit stuck at this part since I want to compare the total size of each array versus the real size of the array (by real size I mean non-repeating numbers)</p>
  
  <p><strong>OUTPUT</strong></p>
  
  <p>This is how the output of the query should look like:</p>
  
  <pre><code>{"_id":ObjectId("2786872873872"), "isRepeating": true}
  {"_id":ObjectId("2786872855555"), "isRepeating": false}
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I've improved my query in order to get this output schema:</p>
  
  <pre><code>db.mycollection.aggregate(
      [
          {$match: {"data_shop.records_data.qty.1": {$lte: 1}}},
          {
              $project: {Album_Cluster:"$data_shop.records_data"}
          },
          {
              $unwind: "$Album_Cluster"
          },
          {
              $project: {qty: "$Album_Cluster.qty"},
          },
          {
              $project: {qty_size: {$size: "$qty"}, qty:1}
          },
          {$group:
              {
              _id: "$_id",
              totalSize: {$addToSet: "$qty_size"},
              realSize: {$addToSet: "$qty"},
              }
          },
          {$unwind: "$realSize"},
          {
              $project: 
              {
                  totalSize:1,
                  real_count: {$size: "$realSize"}
              }
          },
          {$unwind: "$totalSize"},
          {
              $group: {
                  _id: "$_id",
                  total_size: {$addToSet: "$totalSize"},
                  real_size: {$addToSet: "$real_count"}
              }
          },
      ],
      {allowDiskUse: true}
  )
  </code></pre>
  
  <p>And now I'm getting this as my output:</p>
  
  <pre><code>{"_id":ObjectId("2786872873872"), "total_size": [4, 5], "real_size":[4]}
  {"_id":ObjectId("2786872855555"), "total_size": [4, 5], "real_size":[5, 4]}
  </code></pre>
  
  <p>Now my question is, does <code>$in</code> allow me to validate that [4, 5] is valid in [5, 4] so my output will be <code>isRepeating = false</code>?</p>