title: How to build MongoDB pipeline and append stages to the pipeline using .Net Core MongoDB Driver
body: <p>this is my first time creating a system using Net Core and MongoDB, I manage to build fully working CRUD with sort and pagination by sending all input parameter in controller to the service layer to query from DATABASE.</p>
  <p>this is my input parameter for Get Action...</p>
  <pre><code> **public IActionResult Get(string userID, string sellerID, DateTime? dateFrom, DateTime? dateTo, string search, string sortValue, string filter, int offset = 0, int limit = 10)**
  </code></pre>
  <p>I succeeded at creating FilterdefinitionBuilder and SortDefinitionBuilder</p>
  <pre><code>**//MongoDB Definition Builder
  #region MongoDB Definition builder
  FilterDefinitionBuilder&lt;OrderModel&gt; filterBuilder = Builders&lt;OrderModel&gt;.Filter;
  FilterDefinition&lt;OrderModel&gt; filterDefinition = filterBuilder.Empty;
               
  SortDefinitionBuilder&lt;OrderModel&gt; sortBuilder = Builders&lt;OrderModel&gt;.Sort;
  SortDefinition&lt;OrderModel&gt; sortDefinition = sortBuilder.Ascending(x =&gt; x.ID);
  #endregion**
  </code></pre>
  <p>#example 1</p>
  <pre><code>**if (sortValue.ToUpper() == &quot;DESC&quot;)
  {
       sortDefinition = sortDefinition.Descending(x =&gt; x.ID);
  }**
  </code></pre>
  <p>#Example 2</p>
  <p>**//Validate date range</p>
  <pre><code>#region dateFrom and dateTo
  if (dateFrom.HasValue &amp;&amp; dateTo.HasValue)
  {
      if (dateFrom.Value &lt; dateTo.Value.AddDays(1))
      {
      filterDefinition = filterDefinition &amp; filterBuilder.Gte(x =&gt; x.CreatedDate, dateFrom.Value) &amp; 
      filterBuilder.Lt(y =&gt; y.CreatedDate, dateTo.Value.AddDays(1));
      }
  }
  #endregion**
  </code></pre>
  <p>What I wish to accomplish is creating a empty pipeline using builder similarly to what I did with sortDefinition and filterDefinition, and then when certain conditions are met, create a pipeline stage and add into the pipeline.</p>
  <p>I have been surfing online for the answer to no avail but to my little understanding, I believe that I can remove both sortDefinition and filterDefinition and just use stages available in pipeline to accomplish CRUD,sort and pagination by using stages e.g. &quot;Match&quot;,&quot;sort&quot;,&quot;skip&quot;,&quot;limit&quot;. But till now I still don't know the right syntax</p>
  <p><a href="https://i.stack.imgur.com/j4y38.png" rel="nofollow noreferrer">Pipeline choices suggested by Intellisense</a></p>
  <p>kindly asking if anyone knows how to accomplish this.</p>
  <p>Regard,
  Hazmin</p>