Skip to content

Instantly share code, notes, and snippets.

@jungrok5
Forked from a3dho3yn/mongodb_c#_cheatsheet.md
Created February 9, 2022 16:22
Show Gist options
  • Save jungrok5/031aa1cc445b4d3c3cf60f39a013e3c2 to your computer and use it in GitHub Desktop.
Save jungrok5/031aa1cc445b4d3c3cf60f39a013e3c2 to your computer and use it in GitHub Desktop.

Revisions

  1. @a3dho3yn a3dho3yn revised this gist Feb 21, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,8 @@ public class User {
    public Address Address { get; set; }
    public List<string> AliasNames { get; set; }
    public List<Account> Accounts { get; set; }
    DateTime CreatedOn { get; set; }
    DateTime ModifiedOn {get; set; }
    }
    ```

  2. @a3dho3yn a3dho3yn revised this gist Feb 21, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -208,7 +208,7 @@ var updateModification = builder.CurrentDate(x => x.ModifiedOn);
    ```
    There are Mul(tiply), BitwiseAnd, BitwiseOr and BitwiseXor that can be used just like Set operator.

    ### Min/Max
    #### Min/Max
    Min and Max operators will modify fields toward min/max value. So, they will update field *only if* it's value is respectivele greater/less than provided value.
    ```csharp
    var min = builder.Min(u => u.Age, 18) // Updates field if Age > 18
    @@ -239,7 +239,7 @@ var addToSet = builder.AddToSet(u => u.AliasNames, "a3d");
    ```
    AddToSet, deals with arrays as sets, and adds elements to an array *only if* it do not already exist.

    ### Set On Insert
    #### Set On Insert
    Provided value only used on insert. Then, setting upsert flag and using SetOnInsert is equal to *Find Or Create*.
    ```csharp
    var setOnInsert = builder.SetOnInsert(u => u.Name, "Farid");
  3. @a3dho3yn a3dho3yn revised this gist Feb 21, 2017. 1 changed file with 51 additions and 15 deletions.
    66 changes: 51 additions & 15 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com), (C) 2017 by [AppsOn](https://blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. And it doesn't cover connecting to MongoDB, creating indexes, etc.
    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. In addition, it has some deficiencies like connecting to MongoDB, creating indexes, etc.
    This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.

    ## Setup
    @@ -177,18 +177,25 @@ var builder = Builders<User>.Projection;
    var project = builder.Include(u => u.Name).Exclude(u => u.Id);
    var cursor = collection.Find(filter).Project(project);
    ```
    Alternative way to make projections is using Expressions: `builder.Expression(u => new { u.Name });`.
    An alternative way to make projections is using Expressions: `builder.Expression(u => new { u.Name });`.

    ## Update
    We use update definition builder to define which fields and how will change.
    Update, needs a filter definitin to find document and an update definition to define which fields and how will change.

    Updates can affect one or many documents:
    ```csharp
    var builder = Builders<User>.Update;
    var cursor = collection.UpdateOne(filter, update);
    var cursor = collection.UpdateMany(filter, update);
    ```

    ### Update Definition
    ```csharp
    var builder = Builders<User>.Update;
    ```
    #### Basic Updates
    ```csharp
    // Set field
    var setName = builder.Set(u => u.Name, "Hossein").
    var setName = builder.Set(u => u.Name, "Hossein");

    // Unset field
    var unsetName = builder.UnSet(u => u.Name);
    @@ -197,24 +204,53 @@ var unsetName = builder.UnSet(u => u.Name);
    var incAge = builder.Inc(u => u.Age);

    // Current Date
    var m
    var updateModification = builder.CurrentDate(x => x.ModifiedOn);
    ```
    There are Mul(tiply), BitwiseAnd, BitwiseOr and BitwiseXor that can be used just like Set operator.

    #### Update Array Fields
    ### Min/Max
    Min and Max operators will modify fields toward min/max value. So, they will update field *only if* it's value is respectivele greater/less than provided value.
    ```csharp
    var min = builder.Min(u => u.Age, 18) // Updates field if Age > 18
    var max = builder.Max(u => u.Age, 18) // Updates field if Age < 18
    ```

    ### Upsert
    upsert,
    setOnInsert
    #### Rename Field
    ```csharp
    var rename = builder.Rename(u => u.Id, "UserId");
    ```

    ### Bulk Update
    Updates can affect one or many documents
    #### Update Array Fields
    ```csharp
    var cursor = collection.UpdateOne(filter, update);
    var cursor = collection.UpdateMany(filter, update);
    // Push
    var push = builder.Push(u => u.AliasNames, "a3d");

    // Pop
    var pop = builder.Pop(u => u.AliasNames);

    // Pull: removes elements matching value
    var pull = builder.Pull(u => u.AliasNames, "a3d");

    // Pull All: removes elements matches any values in list
    var pullAll = builder.PullAll(u => u.AliasNames, new [] { "a3dho3yn", "hossein" });

    // Add To Set
    var addToSet = builder.AddToSet(u => u.AliasNames, "a3d");
    ```
    AddToSet, deals with arrays as sets, and adds elements to an array *only if* it do not already exist.

    ### Set On Insert
    Provided value only used on insert. Then, setting upsert flag and using SetOnInsert is equal to *Find Or Create*.
    ```csharp
    var setOnInsert = builder.SetOnInsert(u => u.Name, "Farid");
    ```

    ### Combine Updates
    ### Upsert
    Upsert, will _insert_ new document if it fails in finding a document.
    ```csharp
    var options = new UpdateOptions() {IsUpsert = true};
    collection.UpdateOne(filter, update, options);
    ```

    ### Replace (Wholesale Update)
    ```csharp
  4. @a3dho3yn a3dho3yn revised this gist Feb 21, 2017. 1 changed file with 53 additions and 26 deletions.
    79 changes: 53 additions & 26 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com), (C) 2017 by [AppsOn](https://blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. And it doesn't cover connecting to MongoDB, creating indexes, etc.
    This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.

    ## Setup
    @@ -13,21 +13,12 @@ This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.
    public class User {
    [BsonId]
    public int Id { get; set } // It's better to use ObjectId, we used int for simplicity
    public int Age { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public List<string> AliasNames { get; set; }
    public List<Account> Accounts { get; set; }
    }

    [BsonIgnoreExtraElements]
    public class Product {
    [BsonId]
    public int Id { get; set; }
    public string Item { get; set; }
    public int Qty { get; set; }
    public float Price { get; set; }
    public List<string> Colors { get; set }
    }
    ```

    ### Connect To Database
    @@ -125,13 +116,14 @@ var stateFilter = builder.Eq(u => u.Address.State, "OR");
    // Filter by array element
    var aliasFilter = builder.AnyEq(u => u.AliasNames, "a3dho3yn");

    // Filter by array element
    // Filter by array element's field
    var accountFilter = builder.ElemMatch(u => u.Accounts, acc => acc.Provider == "GitHub");

    // Filter by array element in desired values
    var aliasFilter = builder.AnyIn(u => u.AliasNames, new [] { "a3dho3yn", "hossein" });

    // Filter by all array elements
    var aliasFilterAll = builder.All(u => u.AliasNames, new [] {"a3dho3yn", "hossein});
    var aliasFilterAll = builder.All(u => u.AliasNames, new [] { "a3dho3yn", "hossein" });
    ```
    There are AnyXx modifiers, where Xx is Gt, Lt, ... (just like single field filters).

    @@ -172,32 +164,67 @@ List<User> users = cursor.ToList();
    ```

    ### Sort, Skip, Limit
    We use sort definition builder to define sort order.
    ```csharp
    var collection = db.GetCollection<Product>("products");
    var sort = Builders<Product>.Sort.Ascending(p => p.Qty).Descending(p => p.Price);
    var sort = Builders<User>.Sort.Ascending(u => u.Name).Descending(u => u.Age);
    var cursor = collection.Find(filter).Sort(sort).Skip(5).Limit(10);
    ```

    ### Projection
    We use porojection definition builder to define how documents will be represented.
    ```csharp
    var builder = Builders<User>.Projection;
    var project = builder.Include(p => p.Price).Exclude(p => p.Qty);
    var project = builder.Include(u => u.Name).Exclude(u => u.Id);
    var cursor = collection.Find(filter).Project(project);
    ```
    Alternative way to make projections is using Expressions: `builder.Expression(p => new { p.Price });`.
    Alternative way to make projections is using Expressions: `builder.Expression(u => new { u.Name });`.

    ## Update
    ### Replace
    We use update definition builder to define which fields and how will change.
    ```csharp
    var builder = Builders<User>.Update;
    ```
    ### Update Definition
    #### Basic Updates
    ```csharp
    // Set field
    var setName = builder.Set(u => u.Name, "Hossein").

    // Unset field
    var unsetName = builder.UnSet(u => u.Name);

    // Increment field
    var incAge = builder.Inc(u => u.Age);

    // Current Date
    var m
    ```

    #### Update Array Fields

    ### Upsert
    upsert,
    setOnInsert

    ### Bulk Update
    Updates can affect one or many documents
    ```csharp
    var cursor = collection.UpdateOne(filter, update);
    var cursor = collection.UpdateMany(filter, update);
    ```


    ### Combine Updates

    ### Replace (Wholesale Update)
    ```csharp
    var product = new Product() {
    var user = new User() {
    Id = 10,
    Type = "office",
    Qty = 5,
    Price = 2.99,
    Colors = new[] {"red", "green", "blue"}
    Name = "Hamed",
    Email = "[email protected]",
    };
    var filter = Builders<Product>("products").Filter.Eq(u => u.Id, 10);
    collection.ReplaceOne(filter, product);
    var filter = Builders<User>.Filter.Eq(u => u.Id, 10);
    collection.ReplaceOne(filter, user);
    ```

    ## Remove
  5. @a3dho3yn a3dho3yn revised this gist Feb 21, 2017. 1 changed file with 36 additions and 18 deletions.
    54 changes: 36 additions & 18 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,9 @@ As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is
    This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.

    ## Setup
    *Note:* Defined models and collections will be used in entire cheatsheet.

    ### Define Document Model

    ### Define Document Models
    *Note:* Defined models and collections will be used in entire cheatsheet.
    ```csharp
    [BsonIgnoreExtraElements]
    public class User {
    @@ -32,19 +31,33 @@ public class Product {
    ```

    ### Connect To Database
    ```csharp
    // Default connection, localhost:27017
    var db = MongoClient();

    ### Get Collection
    // With Connection String
    var db = MongoClient("http://localhost:27017");

    // With MongoUrl Builder
    var url = MongoUrl.Create("http://localhost:27017");
    var db = MongoClient(url);
    ```

    ### Get Collection
    ```csharp
    var collection = db.GetCollection<User>("users");
    ```

    ## Insert
    ### Basic Insert
    ```csharp
    var collection = db.GetCollection<User>("users");
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };

    // Basic Insert
    collection.InsertOne(user);

    // With Write Concern
    @@ -54,11 +67,11 @@ collection.WithWriteConcern(WriteConcern.WMajority).InsertOne(user);
    ### Insert w/ ID
    ```csharp
    var user = new User {
    Id = 10, //[BsonId] applied
    Id = 10,
    Name = "Hossein",
    Email = "[email protected]"
    }
    collecion.InsertOne(user);
    collection.InsertOne(user);
    ```

    ### Insert Nested
    @@ -76,44 +89,51 @@ collection.InsertOne(user);
    ```
    ### Bulk Insert
    ```csharp
    var collection = db.GetCollection<Product>("products");
    var products = new []
    var users = new []
    {
    new Product { Item = "pencil", Qty = 5 },
    new Product { Item = "pencil", Qty = 20 },
    new Product { Item = "eraser", Qty = 25 }
    new User { Name = "Danial", Email = "[email protected]" },
    new User { Name = "Bahar", Email = "[email protected]" },
    new User { Name = "Shadi", Email = "[email protected]" }
    };
    collection.InsertMany(products);
    collection.InsertMany(users);
    ```

    ## Filters
    Find, update and removing documents use filters, which can be made by filter definition builder.

    ### Basic Filter
    ```csharp
    var builder = Builders<User>.Filter;
    ```

    ### Basic Filter
    ```csharp
    // Empty Filter (matches all)
    var empty = builder.Empty;

    // Filter by field
    var idFilter = builder.Eq(u => u.Id, 10);

    // Filter by field with list of desired values
    var idListFilter = builder.In(u => u.Id, new [] { 10, 14 });
    ```
    You can use `Gt` (<), `Lt` (>), `Ne` (!=), `Lte` (<=), `Gte` (>=) filters, just like `Eq` (==).

    ### Embedded Document/Array Filter
    ```csharp
    // Filter by embedded document's field
    var stateFilter = builder.Eq(u => u.Address.State, "OR");

    // Filter by array element
    var aliasFilter = builder.AnyEq(u => u.AliasNames, "a3dho3yn"); // User.AliasNames is string[]
    var aliasFilter = builder.AnyEq(u => u.AliasNames, "a3dho3yn");

    // Filter by array element
    // Filter by array element's field
    var accountFilter = builder.ElemMatch(u => u.Accounts, acc => acc.Provider == "GitHub");

    // Filter by all array elements
    var aliasFilterAll = builder.All(u => u.AliasNames, new [] {"a3dho3yn", "hossein});
    ```
    There are AnyXx modifiers, where Xx is Gt, Lt, ... (just like single field filters).

    ### Field Properties Filter
    ```csharp
    @@ -133,7 +153,6 @@ var nameFilter = builder.RegEx(u => u.Name, pattern);

    ### Combine Filters
    You can combine filters with bitwise operators (e.g. `&`, `|`) or use builders' methods.

    ```csharp
    var idAndStateFilter = builder.And(new [] {idFilter, stateFilter}); // == idFilter & stateFilter
    var idOrStateFilter = builder.Or(new [] {idFilter, stateFilter}); // == idFilter | stateFilter
    @@ -143,7 +162,6 @@ var idOrStateFilter = builder.Or(new [] {idFilter, stateFilter}); // == idFilter
    ### Basic Find
    Find command will return an iterator, which can be used to retrieve documents.
    ```csharp
    var collection = db.GetCollection<User>("users");
    var cursor = collection.Find(stateFilter);

    // Find One
  6. @a3dho3yn a3dho3yn revised this gist Feb 20, 2017. 1 changed file with 45 additions and 2 deletions.
    47 changes: 45 additions & 2 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,41 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com)*, *(C) 2017 by [AppsOn](https://blog.appson.tech)*
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com), (C) 2017 by [AppsOn](https://blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.

    ## Setup
    *Note:* Defined models and collections will be used in entire cheatsheet.

    ### Define Document Model

    ```csharp
    [BsonIgnoreExtraElements]
    public class User {
    [BsonId]
    public int Id { get; set } // It's better to use ObjectId, we used int for simplicity
    public string Name { get; set; }
    public Address Address { get; set; }
    public List<string> AliasNames { get; set; }
    public List<Account> Accounts { get; set; }
    }

    [BsonIgnoreExtraElements]
    public class Product {
    [BsonId]
    public int Id { get; set; }
    public string Item { get; set; }
    public int Qty { get; set; }
    public float Price { get; set; }
    public List<string> Colors { get; set }
    }
    ```

    ### Connect To Database

    ### Get Collection


    ## Insert
    ### Basic Insert
    ```csharp
    @@ -137,7 +169,18 @@ var cursor = collection.Find(filter).Project(project);
    Alternative way to make projections is using Expressions: `builder.Expression(p => new { p.Price });`.

    ## Update

    ### Replace
    ```csharp
    var product = new Product() {
    Id = 10,
    Type = "office",
    Qty = 5,
    Price = 2.99,
    Colors = new[] {"red", "green", "blue"}
    };
    var filter = Builders<Product>("products").Filter.Eq(u => u.Id, 10);
    collection.ReplaceOne(filter, product);
    ```

    ## Remove
    ### Basic Remove
  7. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 13 additions and 6 deletions.
    19 changes: 13 additions & 6 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,16 @@ This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.
    ## Insert
    ### Basic Insert
    ```csharp
    var collection = db.GetCollection<User>("users");
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);
    collection.InsertOne(user);

    // With Write Concern
    db.GetCollection<User>("users").WithWriteConcern(WriteConcern.WMajority).InsertOne(user);
    collection.WithWriteConcern(WriteConcern.WMajority).InsertOne(user);
    ```

    ### Insert w/ ID
    @@ -25,7 +26,7 @@ var user = new User {
    Name = "Hossein",
    Email = "[email protected]"
    }
    db.GetCollection<User>("users").InsertOne(user);
    collecion.InsertOne(user);
    ```

    ### Insert Nested
    @@ -39,17 +40,18 @@ var user = new User {
    Zip = "97232"
    }
    }
    db.GetCollection<User>("users").InsertOne(user);
    collection.InsertOne(user);
    ```
    ### Bulk Insert
    ```csharp
    var collection = db.GetCollection<Product>("products");
    var products = new []
    {
    new Product { Item = "pencil", Qty = 5 },
    new Product { Item = "pencil", Qty = 20 },
    new Product { Item = "eraser", Qty = 25 }
    };
    db.GetCollection<Product>("products").InsertMany(products);
    collection.InsertMany(products);
    ```

    ## Filters
    @@ -138,7 +140,12 @@ Alternative way to make projections is using Expressions: `builder.Expression(p


    ## Remove
    ### Basic Remove
    ```csharp
    collection.DeleteOne(filter);
    collection.DeleteMany(filter);
    ```

    ### Remove One
    ```csharp
    collection.DeleteOne(filter);
    ```
  8. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com)*, *(C) 2017 by [AppsOn](https://blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.
    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    This updated version works fine with C# Driver v2.4.7 and MongoDB v3.4.

    ## Insert
    ### Basic Insert
    @@ -121,7 +122,6 @@ List<User> users = cursor.ToList();
    ### Sort, Skip, Limit
    ```csharp
    var collection = db.GetCollection<Product>("products");
    var filter = Builders<Product>.Filter.Empty;
    var sort = Builders<Product>.Sort.Ascending(p => p.Qty).Descending(p => p.Price);
    var cursor = collection.Find(filter).Sort(sort).Skip(5).Limit(10);
    ```
    @@ -138,5 +138,7 @@ Alternative way to make projections is using Expressions: `builder.Expression(p


    ## Remove


    ```csharp
    collection.DeleteOne(filter);
    collection.DeleteMany(filter);
    ```
  9. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 61 additions and 4 deletions.
    65 changes: 61 additions & 4 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -54,6 +54,7 @@ db.GetCollection<Product>("products").InsertMany(products);
    ## Filters
    Find, update and removing documents use filters, which can be made by filter definition builder.

    ### Basic Filter
    ```csharp
    var builder = Builders<User>.Filter;

    @@ -62,7 +63,10 @@ var empty = builder.Empty;

    // Filter by field
    var idFilter = builder.Eq(u => u.Id, 10);
    ```

    ### Embedded Document/Array Filter
    ```csharp
    // Filter by embedded document's field
    var stateFilter = builder.Eq(u => u.Address.State, "OR");

    @@ -73,13 +77,66 @@ var aliasFilter = builder.AnyEq(u => u.AliasNames, "a3dho3yn"); // User.AliasNam
    var accountFilter = builder.ElemMatch(u => u.Accounts, acc => acc.Provider == "GitHub");

    // Filter by all array elements
    // Combine filters (and, or)
    // Gt/Lt, Exists,Type, RegExp,
    var aliasFilterAll = builder.All(u => u.AliasNames, new [] {"a3dho3yn", "hossein});
    ```

    ### Field Properties Filter
    ```csharp
    // Filter by field existance
    var hasNameFieldFilter = builder.Exists(u => u.Name);

    // Filter by field type
    var idTypeFilter = builder.Type(u => u.Id, BsonType.ObjectId);
    ```

    ### Regular Expression Filter
    ```csharp
    // Filter with regular expression
    var pattern = new BsonRegularExpression("yn$");
    var nameFilter = builder.RegEx(u => u.Name, pattern);
    ```

    ### Combine Filters
    You can combine filters with bitwise operators (e.g. `&`, `|`) or use builders' methods.

    ```csharp
    var idAndStateFilter = builder.And(new [] {idFilter, stateFilter}); // == idFilter & stateFilter
    var idOrStateFilter = builder.Or(new [] {idFilter, stateFilter}); // == idFilter | stateFilter
    ```

    ## Projection
    ## Sort, Skip, Limit
    ## Find
    ### Basic Find
    Find command will return an iterator, which can be used to retrieve documents.
    ```csharp
    var collection = db.GetCollection<User>("users");
    var cursor = collection.Find(stateFilter);

    // Find One
    var user = cursor.FirstOrDefault();

    // Find All
    List<User> users = cursor.ToList();
    ```

    ### Sort, Skip, Limit
    ```csharp
    var collection = db.GetCollection<Product>("products");
    var filter = Builders<Product>.Filter.Empty;
    var sort = Builders<Product>.Sort.Ascending(p => p.Qty).Descending(p => p.Price);
    var cursor = collection.Find(filter).Sort(sort).Skip(5).Limit(10);
    ```

    ### Projection
    ```csharp
    var builder = Builders<User>.Projection;
    var project = builder.Include(p => p.Price).Exclude(p => p.Qty);
    var cursor = collection.Find(filter).Project(project);
    ```
    Alternative way to make projections is using Expressions: `builder.Expression(p => new { p.Price });`.

    ## Update


    ## Remove


  10. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)*, *(C) 2017 by [AppsOn](blog.appson.tech)*
    *(C) 2015 by [Derek Hunziker](http://www.layerworks.com)*, *(C) 2017 by [AppsOn](https://blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.

  11. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 23 additions and 4 deletions.
    27 changes: 23 additions & 4 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -54,13 +54,32 @@ db.GetCollection<Product>("products").InsertMany(products);
    ## Filters
    Find, update and removing documents use filters, which can be made by filter definition builder.

    `csharp
    var builder = Builders<User>.Filter;`
    ```csharp
    var builder = Builders<User>.Filter;

    // Empty Filter (matches all)
    var empty = builder.Empty;

    // Filter by field
    builder.Eq(u => u.Id, 10);
    var idFilter = builder.Eq(u => u.Id, 10);

    // Filter by embedded document's field
    var stateFilter = builder.Eq(u => u.Address.State, "OR");

    // Filter by nested
    // Filter by array element
    var aliasFilter = builder.AnyEq(u => u.AliasNames, "a3dho3yn"); // User.AliasNames is string[]
    // Filter by array element's field
    var accountFilter = builder.ElemMatch(u => u.Accounts, acc => acc.Provider == "GitHub");

    // Filter by all array elements
    // Combine filters (and, or)
    // Gt/Lt, Exists,Type, RegExp,
    ```

    ## Projection
    ## Sort, Skip, Limit
    ## Update
    ## Remove


  12. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 19 additions and 11 deletions.
    30 changes: 19 additions & 11 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -12,19 +12,23 @@ var user = new User
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);
    ```
    // 1.2. With Write Concern

    // With Write Concern
    db.GetCollection<User>("users").WithWriteConcern(WriteConcern.WMajority).InsertOne(user);
    ```

    // 1.3. Insert w/ ID
    ### Insert w/ ID
    ```csharp
    var user = new User {
    Id = 10, // [BsonId] applied
    Id = 10, //[BsonId] applied
    Name = "Hossein",
    Email = "[email protected]"
    }
    db.GetCollection<User>("users").InsertOne(user);
    ```

    // 1.4. Insert Nested
    ### Insert Nested
    ```csharp
    var user = new User {
    Name = "Hossein",
    Email = "[email protected]",
    @@ -35,24 +39,28 @@ var user = new User {
    }
    }
    db.GetCollection<User>("users").InsertOne(user);

    // 1.5. Bulk Insert
    ```
    ### Bulk Insert
    ```csharp
    var products = new []
    {
    new Product { Item = "pencil", Qty = 5 },
    new Product { Item = "pencil", Qty = 20 },
    new Product { Item = "eraser", Qty = 25 }
    };
    db.GetCollection<Product>("products").InsertMany(products);
    ```

    ## Filters
    Find, update and removing documents use filters, which can be made by filter definition builder.

    //---------------- Filters -------------------
    // Find, uses filters, which can be made by Builders or Expressions. We use Builders.
    var builder = Builders<User>.Filter;
    `csharp
    var builder = Builders<User>.Filter;`

    // Filter by field
    builder.Eq(u => u.Id, 10);

    //
    // Filter by nested



  13. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)*

    *(C) 2017 by [AppsOn](blog.appson.tech)*
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)*, *(C) 2017 by [AppsOn](blog.appson.tech)*

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.

  14. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)*

    *(C) 2017 by [AppsOn](blog.appson.tech)
    *(C) 2017 by [AppsOn](blog.appson.tech)*

    >> As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.
    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.

    ## Insert
    ### Basic Insert
  15. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,16 @@

    >> As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.
    ## Read
    ### 1. Insert
    #### 1.1. Basic Insert
    ## Insert
    ### Basic Insert
    ```csharp
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);

    ```
    // 1.2. With Write Concern
    db.GetCollection<User>("users").WithWriteConcern(WriteConcern.WMajority).InsertOne(user);

  16. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)

    *(C) 2017 by [AppsOn](blog.appson.tech)

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    This updated version based on MongoDB v3.4 and C # Driver v2.4.7.
    >> As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated. This updated version based on MongoDB v3.4 and C # Driver v2.4.7.
    ## Read
    ### 1. Insert
  17. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,9 @@
    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    This updated version based on MongoDB v3.4 and C # Driver v2.4.7.

    # Read
    ## 1. Insert
    ### 1.1. Basic Insert
    ## Read
    ### 1. Insert
    #### 1.1. Basic Insert
    var user = new User
    {
    Name = "Hossein",
  18. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 2 changed files with 60 additions and 15 deletions.
    15 changes: 0 additions & 15 deletions mongodb_c#_cheatsheet.cs
    Original file line number Diff line number Diff line change
    @@ -1,15 +0,0 @@
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };

    // Basic Insert
    db.GetCollection<User>("users").InsertOne(user);

    // Insert w/ ID
    user = new User {
    Id = 10, // [BsonId] applied
    Name = "Hossein",
    Email = "[email protected]"
    }
    60 changes: 60 additions & 0 deletions mongodb_c#_cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    # MongoDB C# Driver Cheat Sheet
    *(C) 2015 by [Derek Hunziker](www.layerworks.com)
    *(C) 2017 by [AppsOn](blog.appson.tech)

    As of releasing MongoDB 3.4 and C# Driver v2.4, original cheatsheet by Derek is outdated.
    This updated version based on MongoDB v3.4 and C # Driver v2.4.7.

    # Read
    ## 1. Insert
    ### 1.1. Basic Insert
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);

    // 1.2. With Write Concern
    db.GetCollection<User>("users").WithWriteConcern(WriteConcern.WMajority).InsertOne(user);

    // 1.3. Insert w/ ID
    var user = new User {
    Id = 10, // [BsonId] applied
    Name = "Hossein",
    Email = "[email protected]"
    }
    db.GetCollection<User>("users").InsertOne(user);

    // 1.4. Insert Nested
    var user = new User {
    Name = "Hossein",
    Email = "[email protected]",
    Address = new Address {
    City = "Portland",
    State = "OR",
    Zip = "97232"
    }
    }
    db.GetCollection<User>("users").InsertOne(user);

    // 1.5. Bulk Insert
    var products = new []
    {
    new Product { Item = "pencil", Qty = 5 },
    new Product { Item = "pencil", Qty = 20 },
    new Product { Item = "eraser", Qty = 25 }
    };
    db.GetCollection<Product>("products").InsertMany(products);

    //---------------- Filters -------------------
    // Find, uses filters, which can be made by Builders or Expressions. We use Builders.
    var builder = Builders<User>.Filter;

    // Filter by field
    builder.Eq(u => u.Id, 10);

    //



  19. @a3dho3yn a3dho3yn revised this gist Feb 19, 2017. 2 changed files with 15 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);
    15 changes: 15 additions & 0 deletions mongodb_c#_cheatsheet.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };

    // Basic Insert
    db.GetCollection<User>("users").InsertOne(user);

    // Insert w/ ID
    user = new User {
    Id = 10, // [BsonId] applied
    Name = "Hossein",
    Email = "[email protected]"
    }
  20. @a3dho3yn a3dho3yn created this gist Feb 19, 2017.
    6 changes: 6 additions & 0 deletions cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    var user = new User
    {
    Name = "Hossein",
    Email = "[email protected]"
    };
    db.GetCollection<User>("users").InsertOne(user);