title: MongoDB text search with sorting in C#
body: <p>I'm trying to implement the text search in MongoDB, in C#.</p>
  
  <p>The documentation doesn't cover anything about how to sort the text search results in C#.</p>
  
  <p>I have a list of tags, separated by spaces, to match.<br />
  If I provide a string like "Tag1 Tag2", I would like the results to be provided in the following order:</p>
  
  <ol>
  <li>Anything that has BOTH 'Tag1' AND 'Tag2', followed by</li>
  <li>Anything that has only 'Tag1', followed by</li>
  <li>Anything that has only 'Tag2'</li>
  </ol>
  
  <p>I have been trying to piece together some code:</p>
  
  <pre><code>var F = Builders&lt;MongoPost&gt;.Filter.Text(Tags);
  var S = Builders&lt;MongoPost&gt;.Sort.MetaTextScore("textScore");
  return Mongo.Driver.Find(F).Sort(S).ToListAsync().Result;
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <blockquote>
    <p>{"QueryFailure flag was true (response was { \"$err\" : \"Can't canonicalize query: BadValue must have $meta projection for all $meta sort keys\", \"code\" : 17287 })."}</p>
  </blockquote>
  
  <p>No proper documentation for this error...</p>
  
  <p>Then I found the following code on this site:</p>
  
  <pre><code>var pipeline = new List&lt;BsonDocument&gt;
  {
      BsonSerializer.Deserialize&lt;BsonDocument&gt;("{ $match: { $text: { $search: \"" + Tags + "\" } } }"),
      BsonSerializer.Deserialize&lt;BsonDocument&gt;("{ $sort: { score: { $meta: \"textScore\" } } }")
  };
  
  var R = Mongo.Driver.AggregateAsync(new BsonDocumentStagePipelineDefinition&lt;MongoPost, MongoPost&gt;(pipeline)).Result;
  return R.ToListAsync().Result;
  </code></pre>
  
  <p>At least is runs without error, but I am trying to make the code in the style at the top, like you can do for the rest of the API, and not having to go back to console style text strings that have to be parsed for every execution.
  Also, I need to add a lot more criteria to the search, so this syntax is not practical for me.</p>
  
  <p>Is there any proper documentation I missed? If not, does anyone know how to implement this in the style at the top?</p>