title: How to do aggregation sort in mongodb inner object with c#
body: <p>I need to sort inner object sort of a class with mongo db. i need to sort based on marks</p>
  <p><strong>Sample Json:</strong></p>
  <pre><code>{&quot;_id&quot;:&quot;58e46f81c4734559ac8082f0&quot;,&quot;Name&quot;:&quot;test&quot;,&quot;Students&quot;:[{&quot;Name&quot;:&quot;A&quot;,&quot;Marks&quot;:&quot;988&quot;}]}
  {&quot;_id&quot;:&quot;58e46f81c4734559ac8082f1&quot;,&quot;Name&quot;:&quot;sahir&quot;,&quot;Students&quot;:[{&quot;Name&quot;:&quot;sahir&quot;,&quot;Marks&quot;:&quot;311&quot;}]}
  </code></pre>
  <p>Here is the class:</p>
  <pre><code>public class  Student
      {
  
  
          [BsonId]
          public ObjectId Id { get; set; }
      
          [BsonElement]
          public string Name { get; set; }
          
          public ICollection &lt;Students&gt; Students { get; set; }
  
      }
  
      public class Students
      {
      
          public string Name { get; set; }
          
          public string Marks{ get; set; }
  
      }
  </code></pre>
  <p><strong>Action</strong></p>
  <pre><code>public IActionResult Index()
      {
  
  
          //Get the database connection
          mongoDatabase = GetMongoDatabase();
          var x = mongoDatabase.GetCollection&lt;Student&gt;(&quot;student&quot;).Find(FilterDefinition&lt;Student&gt;.Empty).ToList();
  
  
    
          var _dbContext = mongoDatabase.GetCollection&lt;Student&gt;(&quot;student&quot;);
  
  // mongo db document collection student class students objects need to sort based on marks.
          _dbContext.Aggregate&lt;Student&gt;()
                           .Sort(x=&gt; x.students.marks).Skip((page-1)*pageSize).Limit(100);
                            
                            .ToList();         -- how to do that sort in this line 
  
  
  or 
  
  
      var result = _dbContext.Aggregate&lt;Student&gt;().Sort(new BsonDocument(&quot;Marks&quot;, 1)).ToList();
  
  
  
          return View(result);
      }
  </code></pre>
  <p><strong>Note:</strong></p>
  <p><code>Don't provide linq Solution to convert asQuerable operators to do LINQ queries. This question is only based on an understanding of MongoDB aggregation sort and how to use that in C#</code>.</p>
  <p>No forum has any reference for internal objects sorting with c#.There are lots of examples of C# mongodb sorting without aggragtion</p>
  <p><strong>Problem:</strong>
  I have aggregation sort mongo shell query .. don't know to implement that in c#</p>