Tools.Net.Mongo.Core
3.3.0
dotnet add package Tools.Net.Mongo.Core --version 3.3.0
NuGet\Install-Package Tools.Net.Mongo.Core -Version 3.3.0
<PackageReference Include="Tools.Net.Mongo.Core" Version="3.3.0" />
paket add Tools.Net.Mongo.Core --version 3.3.0
#r "nuget: Tools.Net.Mongo.Core, 3.3.0"
// Install Tools.Net.Mongo.Core as a Cake Addin
#addin nuget:?package=Tools.Net.Mongo.Core&version=3.3.0
// Install Tools.Net.Mongo.Core as a Cake Tool
#tool nuget:?package=Tools.Net.Mongo.Core&version=3.3.0
Tools .NET Mongo Core
Overview
A core library for managing MongoDB databases from a .NET application
Getting Started
To get started create a context class that inherits from the MongoDbContext
base class. A context class is used to manage the appication's interactions with MongoDB collections by using class Properties.
MongoDbContext
is an abstract class that establishes a connection to new or existing databases. The context class inhereting from MongoDbContext
needs to contain properties of type IMongoCollection<TDocument> to map to the corresponding MongoDB Collection. The class used for TDocument
defines the document structure for the given MongoDB collection. The data model should use MongoDB Serialization Attributes to define the behavior and data type of each property.
By default, the MongoDB collection names will be the camel cased name of the given IMongoCollection
property in the context class. To override the default naming, use the CollectionName
attribute.
Example
public class Pet
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonIgnoreIfDefault]
public string Type { get; set; }
[BsonIgnoreIfDefault]
public string Breed { get; set; }
[BsonRequired]
public string Name { get; set; }
[BsonRepresentation(BsonType.Int32)]
public int Age { get; set; }
}
public class VeterinaryDbContext : MongoDbContext
{
public IMongoCollection<Pet> Pets { get; set; }
public VeterinaryDbContext(IMongoDbContext context) : base(connectionString)
{
}
}
A MongoDB context class represents the connection to a single MongoDB database. To establish a connection to the MongoDB database just pass the connection string into the constructor.
var vetContext = new VeterinaryDbContext("mongodb://localhost:27017/petDatabase");
Once the connection is established, any of the MongoDB operation can be called from any IMongoCollection
property.
var insertResult = vetContext.Pets.InsertOne(pet);
For direct IMongoDatabase access, use the MongoDbContext.Db
property.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- MongoDB.Driver (>= 2.21.0)
-
net7.0
- MongoDB.Driver (>= 2.21.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.3.0 | 173 | 10/7/2023 |
3.2.0 | 133 | 5/13/2023 |
3.1.0 | 530 | 4/29/2022 |
3.0.0 | 534 | 4/14/2022 |
2.0.0 | 598 | 1/22/2022 |
2.0.0-rc | 360 | 12/24/2019 |
2.0.0-beta | 360 | 12/23/2019 |
1.0.0 | 547 | 9/13/2019 |
1.0.0-alpha | 382 | 8/31/2019 |