title: MongoDB Documents : Find number of Repetitions and Sort In Ascending Order
body: <p>So I'm working on an agent that's supposed get data from <strong>MongoDB</strong>, iterate through it, and then find the keys which have the same values, and sort that list in ascending order. Basically, find out how many repetitions there are in the documents from Mongo, and then sort it.</p>
  
  <p>So I've gotten to the part where I get repetitions in the data, and count them, but I'm stuck with getting the ascending order part.</p>
  
  <hr>
  
  <hr>
  
  <p>The code for getting documents out of <strong>mongodb</strong> and processing it is as follows.</p>
  
  <pre><code>function matchRoomAndAgent() {
  var url = 'mongodb://192.168.10.10:27017';
  
  MongoClient.connect(url, function (err, db) {
      db.collection("whiteboardRooms").find({}).toArray(function (err, res) {
          var roomsArray = [];
          var count = {};
          var temp_count = {};
          if (err) {
              throw err;
          }
          db.close();
          // roomsArray.push(res);
          for (var i = 0; i &lt; res.length; i++) {
              roomsArray.push(res[i]);
              // console.log('agentId:  '+ res[i][agentId].agentId);
          }
  
          // console.log(roomsArray);
  
          for (var i = 0; i &lt; 15; i++) //supposed to use length of roomsArray
          {
              var currentAgent = roomsArray[i].agentId;
              for (var j = 0; j &lt; 15; j++)
              {
                  if (roomsArray[j].agentId == currentAgent)
                  {
                      count[j] = (count[j] || 0) + 1;
                      temp_count[j] = count[j];                     
                  }
              }
              console.log(temp_count);
          }
  </code></pre>
  
  <hr>
  
  <p>Also please find attached, a sample pic of the mongodb documents. </p>
  
  <p><a href="https://i.stack.imgur.com/06irl.png" rel="nofollow noreferrer">MongoDB Documents</a></p>
  
  <p>Please let me know if you require more information, and I'll do my best to get it. I'm kind of a noob at programming. Sorry! </p>
  
  <p>Thanks in advance for your help :)</p>