title: How to write search query for the below MongoDB query scenario in C#
body: <p>Considering below 5 parameters:</p>
  
  <ol>
  <li>Mutual friends</li>
  <li>Home Town</li>
  <li>Current City</li>
  <li>School</li>
  <li>company</li>
  </ol>
  
  <p>I need to write a search query that search should start with matching with all parameters which will get accurate results.</p>
  
  <ul>
  <li>consider at-least 4 parameters</li>
  <li>If not then consider at-least 3 parameters    </li>
  <li>If not then consider at-least 2 parameters   </li>
  <li>If not then consider at-least 1 parameter   </li>
  <li>If not then shows all.</li>
  </ul>
  
  <p>How to write search query for the above scenario in C#?</p>
  
  <p>I used the following code</p>
  
  <pre><code>public class UserDetails
      {
          [BsonId]
          [BsonRepresentation(BsonType.ObjectId)]
          public string _id { get; set; }
          public string UserId { get; set; }
          public Registration RegistrationDetails { get; set; }
       }
  public class Registration
      {
          public string FirstName { get; set; }
          public string LastName { get; set; }
      }
  var collection = database.GetCollection&lt;UserDetails&gt;("UserInfo");
                  var agg = collection.Aggregate().Project(p =&gt; new
                  {
                      p.Id,
                      matchValue =
                      (p.RegistrationDetails.FirstName == search ? 1 : 0) +
                      (p.RegistrationDetails.LastName == search ? 1 : 0)
                  })
                  .Group(new BsonDocument(new Dictionary&lt;string, BsonValue&gt;
                  {
                      {"_id", BsonValue.Create("$matchValue")},
                      {"ids", new BsonDocument("$push", new 
                       BsonDocument("item","$Id"))}
                  }))
                  .Sort(new BsonDocument("_id", BsonValue.Create(-1)))
                  .Limit(1)
                  .Unwind("ids")
                  .Project(new BsonDocument("_id", "$ids.item"));
  </code></pre>