title: Node + Mongodb + sort nested array
body: <p>I am using node with mongodb.I try to sort nested array.</p>
  
  <p>Example json in monogodb.</p>
  
  <pre><code>{
      "items": [
          {
              "name": "A",
              "order": 3
          }, {
              "name": "B",
              "order": 2
          }, {
              "name": "C",
              "order": 1
          }, {
              "name": "D",
              "order": 4
          }
      ],
      "name": "Test",
          "_id" : 01245678913
  }
  {
      "items": [
          {
              "name": "A",
              "order": 3
          }, {
              "name": "B",
              "order": 2
          }, {
              "name": "C",
              "order": 1
          }, {
              "name": "D",
              "order": 4
          }
      ],
      "name": "Test",
          "_id" : 098765432134
  }
  </code></pre>
  
  <p>I am expecting result should display based on order. items.order</p>
  
  <p>Here possible to pass ID. If get Id means display the result for the respective Id. otherwise need to show all lists.</p>
  
  <p>I attached my node code:</p>
  
  <pre><code>router.post('/get', function(req, res, next) {
  
          var results = {
              resp : {},
              id : null,
              query : {}
          };
  
          if (!( typeof req.body['id'] === 'undefined' || req.body['id'] === null )) {
              results.id = req.body['id'];
          }
  
          Q(results).then(function(results) {
              var deferred = Q.defer();
              var collection = mongoUtil.list;
              if (results.id != null) {
                  results.query._id = ObjectId(results.id);
              }
  
  
              collection.find(results.query).toArray(function(err, lists) {
  
                  if (!err &amp;&amp; lists.length &gt; 0) {
                      results.resp.lists = lists;
                      deferred.resolve(results);
                  } else {
  
                      results.resp.message = 'No List';
                      results.resp.debug = err;
                      deferred.reject(results);
                  }
              });
              return deferred.promise;
          }).then(function(results) {
              results.resp.message = 'Result found';
              results.resp.status = 'Success';
              res.send(results.resp);
          }).fail(function(results) {
              results.resp.status = 'Error';
              res.send(results.resp);
          });
  
      });
  </code></pre>
  
  <p>I found few example they using aggregate function.</p>
  
  <pre><code>collection.aggregate([
    {$unwind: "$answers"}, 
    {$sort: {"item.order":1}}, 
    {$group: {_id:"$_id", answers: {$push:"$answers"}}}
  ]);
  </code></pre>
  
  <p>Here little bit confusing.</p>