title: How to sort string with a number in aggregation using mongodb
body: <p>Actually I'm doing sorting for these documents in this <code>name</code> variable is in lowercase and uppercase values. If i apply sorting for these <code>name</code> variable not working properly.</p>
  
  <p>Then I change <code>name</code> variable to lowercase using <code>{ $toLower: '$name' }</code> then sorted fine. then same lower case applying for <code>position</code> variable then it is converting number to string.</p>
  
  <p>In this case sorting is not working properly i cannot able convert or checking type of variable in  aggregation. can you suggest how to check variable type in aggregation.</p>
  
  <pre><code>{
      'name': 'vvvvvv',
      'slug': 'vvvvvv',
      'position': NumberLong(4),
      'status': NumberLong(0),
      'parent': ObjectId("56b9f1948eb55dfc17cf33f0"),
      'category_filter': [  ],
      'seo': {
          'title': 'vvvvvv',
          'keyword': 'vvvvvv',
          'description': 'vvvvvv'
      } 
  },
  {
      'name': 'Person',
      'slug': 'vvvvvv',
      'position': NumberLong(4),
      'status': NumberLong(0),
      'parent': ObjectId("56b9f1948eb55dfc17cf33f0"),
      'category_filter': [  ],
      'seo': {
          'title': 'vvvvvv',
          'keyword': 'vvvvvv',
          'description': 'vvvvvv'
      } 
  }
  </code></pre>
  
  <p><em>Aggregation Query:</em></p>
  
  <pre><code>[
      { $project: {
          _id: 1,
          items: { $size: "$items" },
          status: 1,
          total: 1,
          shop: 1,
          customer: 1,
          dname: { $cond: { 
              if: { 'position': { $type : "string" }}, 
              then: { $toLower: '$'+sorted },
              else: '$'+sorted 
          }}
      }
  ]
  </code></pre>