MongoDB C# docs
GitHub.NET DriverMongoDB docs
  • ⭐Introduction
  • πŸ“ŒGetting started
    • Environment setup
    • πŸƒβ€β™‚οΈ Quick start
      • MongoDB connection
      • Databases
      • Collections
      • Insert documents
      • Read documents
      • Update documents
  • πŸ“‘CRUD Basics
    • βž•Create
      • Id Member
      • Ordered insert
    • πŸ”ŽRead
      • Basics
      • Comparison Operators
      • Logical Operators
      • Element Operators
      • Array operators
      • Evaluation Operators
    • πŸ“Update
      • Operators
      • Replace
      • Arrays
    • ❌Delete
  • πŸ§ͺAggregation
    • Overview
    • βœ‚οΈProject
    • 🎯Match
    • πŸ“¦Group
    • 🚩Unwind
    • ⏩Pagination
    • πŸ“ˆBucket
    • ✨Operators
Powered by GitBook
On this page
  • Get a collection reference
  • Create collections
  • Capped collection
  • List collections
  • Drop collections
  1. πŸ“ŒGetting started
  2. πŸƒβ€β™‚οΈ Quick start

Collections

Get a collection reference

In order to read/write data from/to a database collection, first you need to get a reference to that collection. The collection doesn't have to exist already and if it doesn't, it will be created automatically the very first time you run a read or write operation. Use the GetCollection<T> method on an IMongoDatabase reference to get a collection reference.

IMongoDatabase.GetCollection<T>(string collection)
AccessCollections.cs
// Get a reference to the database
var database = Client
    .GetDatabase(Constants.SamplesDatabase);

// Get a reference to a database's collection named 'users'
var personsTypedCollection = database
    .GetCollection<User>(Constants.UsersCollection);
public class User
    {
        [BsonId]
        [BsonIgnoreIfDefault] // required for replace documents 
        public ObjectId Id { get; set; }
        public Gender Gender { get; set; }
        public string FirstName {get; set; }
        public string LastName {get; set; }
        public string UserName {get; set; }
        public string Avatar {get; set; }
        public string Email {get; set; }
        public DateTime DateOfBirth {get; set; }
        public AddressCard Address {get; set; }
        public string Phone {get; set; }
        
        [BsonIgnoreIfDefault]
        public string Website {get; set; }
        public CompanyCard Company {get; set; }
        public decimal Salary { get; set; }
        public int MonthlyExpenses { get; set; }
        public List<string> FavoriteSports { get; set; }
        public string Profession { get; set; }
    }
// sample doc
{
	"_id" : ObjectId("5e8897d94d749f71d8240a3c"),
	"gender" : 1,
	"firstName" : "Jacqueline",
	"lastName" : "Dickens",
	"userName" : "Jacqueline.Dickens",
	"avatar" : "https://api.adorable.io/avatars/285/abott@adorable.png",
	"email" : "Jacqueline99@gmail.com",
	"dateOfBirth" : ISODate("2000-01-22T09:52:09.032+02:00"),
	"address" : {
		"street" : "65603 Wyatt Roads",
		"suite" : "Suite 220",
		"city" : "South Bernardo",
		"state" : "Kansas",
		"zipCode" : "54687-9947",
		"geo" : {
			"lat" : -2.4298,
			"lng" : 179.5312
		}
	},
	"phone" : "874.533.9433 x9618",
	"website" : "madison.info",
	"company" : {
		"name" : "Lebsack - Robel",
		"catchPhrase" : "Innovative mission-critical local area network",
		"bs" : "engage integrated markets"
	},
	"salary" : 4218,
	"monthlyExpenses" : 2628,
	"favoriteSports" : [
		"American Football",
		"Basketball",
		"Soccer",
		"Baseball",
		"Tennis",
		"Moto GP",
		"Table Tennis",
		"Motor Sport",
		"Beach Volleyball",
		"Snooker",
		"Ice Hockey",
		"Cricket",
		"Volleyball",
		"Cycling",
		"MMA",
		"Water Polo"
	],
	"profession" : "Photographer"
}

Notice that in the above sample the collection reference is a typed collection of type IMongoCollection<User> and most of its methods require parameters of type User. It basically implies that a document stored in the collection will match and can be deserialized by the driver back to a User entity.

This is different than getting a reference collection of type IMongoCollection<BsonDocument>:

var bsonPersonsCollection = database
    .GetCollection<BsonDocument>(Constants.UsersCollection);

With a IMongoCollection<BsonDocument>collection reference, you can build pretty much all MongoDB queries you write in the shell, even the complex ones. The downside though is that you end up writing ugly code that is hard to maintain when models change. Luckily, these docs' goal is to learn you how to build typed MongoDB queries

Create collections

You can create a collection using the CreateCollection method on a IMongoDatabase reference.

IMongoDatabase.CreateCollection(string collection)
AccessCollections.cs
var loginsCollectionName = "logins";
await database
    .CreateCollectionAsync(loginsCollectionName);
db.createCollection("myCollection")

Capped collection

A Capped collection in MongoDB is a special fixed-size collection where when its allocated sized is filled, the oldest documents inserted are automatically removed to make room for the new ones. You can define a collection as capped only during its creation by using the CreateCollectionOptions and setting the values for either (or both) the max documents and the max size. If any max documents or max size exceeds, the latest document will be removed to make room for the next one inserted.

Remember that in case you set the MaxDocuments you are required to set the MaxSize as well

AccessCollections.cs
await database
    .CreateCollectionAsync(Constants.TravelersCollection,
        new CreateCollectionOptions()
        {
            Capped = true, MaxDocuments = 3, MaxSize = 10000
        });
        var travelers = RandomData.GenerateTravelers(3);
travelers.First().Name = "Christos";

var travelersCollection = tripsDatabase
    .GetCollection<Traveler>(travelersCollectionName);

await travelersCollection.InsertManyAsync(travelers);

// Max documents reached - Now let's insert another one
await travelersCollection.InsertManyAsync(RandomData.GenerateTravelers(1));

// First document have been removed to fill room for the new one
db.createCollection("myCollection", { 
    capped : true, 
    size : 50 * 1024 * 1024, 
    max : 100 * 1000 
})

Why a capped collection anyway?

  • Order retrieved is always same as the order inserted. This is not guaranteed with a normal collection

  • Automatic clear up ensures fixed collection size and hence efficient to work with

  • Ideal for scenarios where you want high insertion throughput while you don't care or want to get rid of old data

List collections

You can get all available collections in a database by using the ListCollections method on an IMongoDatabase reference.

IMongoDatabase.ListCollections()
AccessCollections.cs
var database = Client.GetDatabase(Constants.SamplesDatabase);

// Get all collections
var collections = (await database
    .ListCollectionsAsync()).ToList();
{
  "name": "users",
  "type": "collection",
  "options": {},
  "info": {
    "readOnly": false,
    "uuid": "fb0d121f-6113-4e43-95c6-065948b6c9af"
  },
  "idIndex": {
    "v": 2,
    "key": {
      "_id": 1
    },
    "name": "_id_",
    "ns": "Persons.users"
  }
}

Drop collections

Delete a collection using the DropCollection method. The collection along with its data will be entirely removed.

IMongoDatabase.DropCollection(string collection)
var loginsCollectionName = "logins";
await database
    .DropCollectionAsync(loginsCollectionName);
PreviousDatabasesNextInsert documents

Last updated 5 years ago

Reference