title: MongoDB & Web API - can't understand how to use filters
body: <p>I am trying to do basic CRUD on a MongoDB using C# Web API. All the examples online are for deprecated methods and I simply can't get the new methods to work.</p>
  
  <p>the places where i need help in the code bellow:</p>
  
  <ol>
  <li>Delete all items in collection - syntax to remove all.</li>
  <li>get all items in collection - filter for get all</li>
  <li>get by id = syntax to select by id</li>
  </ol>
  
  <p>i've tried many examples online. most are deprecated for previous versions of MongoDB and the official Mongo docs are not helping, i suspect as my code is involving WebAPI classes and the examples are not for that.</p>
  
  <p>thanks for your help!</p>
  
  <p>This is the class: </p>
  
  <pre><code>  public class template
  {
  
      [BsonId]
      public string templateUniqueId { get; set; }
  
      public string outsideClientId { get; set; }
  
      public string ClientId { get; set; }
  
      public string templateFieldsData { get; set; }
  
      public bool isActive { get; set; }
  
  }
  </code></pre>
  
  <p>And my repository implementation (part of if):</p>
  
  <pre><code>public class templateRepository : ItemplateRepository
      {
          public MongoClient client;
          public IMongoDatabase database;
          public IMongoCollection&lt;template&gt; templatesCollection;
          public templateRepository()
          {
              string connectionString = ConfigurationManager.AppSettings["mongoconnection"];
              if (string.IsNullOrWhiteSpace(connectionString))
              {
                  connectionString = "mongodb://localhost:27017";
              }
  
              client = new MongoClient(connectionString);
              database = client.GetDatabase(ConfigurationManager.AppSettings["mongo_personal"]);
  
              templatesCollection = database.GetCollection&lt;template&gt;("templates");
              //var persons = await collection.Find(new BsonDocument()).ToListAsync();
  
  
              // Reset database and add some default entries
              FilterDefinition&lt;template&gt; f;
  
              var result = templatesCollection.DeleteMany(f); // this line should remove all items
  
              for (int index = 1; index &lt; 5; index++)
              {
                  template template1 = new template
                  {
                      templateUniqueId = string.Format("t{0}", index.ToString()),
                      outsideClientId= string.Format("t{0}", index.ToString()),
                      ClientId = string.Format("t{0}", index.ToString()),
                      isActive = true,
                      templateFieldsData = "sharon"
  
                  };
                  AddTemplate(template1);
              }
          }
  
          public void AddTemplate(template templateIn)
          {
              database.GetCollection&lt;template&gt;("templates").InsertOne(templateIn);
          }
  
          public IEnumerable&lt;template&gt; GetAllTemplates()
          {
              var templates = database.GetCollection&lt;template&gt;("templates").Find({ }); // get all templates
              return templates;
          }
  
  
          public template GetTemplate(string id)
          {
              IMongoQuery query = Query.EQ("_id", id);
              return templatesCollection.Find(query).FirstOrDefault();
          }
  
          public bool UpdateTemplate(string id, template item)
          {
              IMongoQuery query = Query.EQ("_id", id);
              item.LastModified = DateTime.UtcNow;
              IMongoUpdate update = Update
                  .Set("outsideClientId", item.outsideClientId)
                 .Set("ClientId", item.ClientId)
                  .Set("isActive", item.isActive)
                 .Set("templateFieldsData", item.templateFieldsData);
              SafeModeResult result = templatesCollection.Update(query, update);
              return result.UpdatedExisting;
          }
      }
  </code></pre>