Skip to content

Instantly share code, notes, and snippets.

View netsharpdev's full-sized avatar
🇵🇱

Paweł Pindel netsharpdev

🇵🇱
View GitHub Profile
services.AddSingleton<IMongoClient>(new MongoClient(Configuration.GetConnectionString("MongoDB")));
"ConnectionStrings": {"MongoDB": "mongodb+srv://<user>:<password>@test.fd2ej.mongodb.net/mongo_test?retryWrites=true&w=majority"}
public class RestaurantsRepository : IRestaurantsRepository
{
private readonly IMongoCollection<Restaurant> collection;
public RestaurantsRepository(IMongoClient mongoClient, IOptions<DbSettings> options)
{
collection = mongoClient.GetDatabase(options.Value.DatabaseName)
.GetCollection<Restaurant>(options.Value.CollectionName);
}
public class Restaurant
{
public string Id { get; set; }
public string Borough { get; set; }
public string Cuisine { get; set; }
public string Name { get; set; }
public string RestaurantId { get; set; }
}
var pack = new ConventionPack();
pack.Add(new CamelCaseElementNameConvention());
ConventionRegistry.Register("Custom Convention", pack, t => t.FullName.StartsWith("MongoTest"));
@netsharpdev
netsharpdev / RegisterClassMap.cs
Last active March 19, 2024 13:18
RegisterClassMap.cs
BsonClassMap.RegisterClassMap<Restaurant>(opt =>
{
opt.AutoMap();
opt.GetMemberMap(c => c.RestaurantId).SetElementName("restaurant_id");
opt.GetMemberMap(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId));
opt.SetIgnoreExtraElements(true);
});
@netsharpdev
netsharpdev / RegisterClassMap.cs
Last active March 6, 2024 10:21
MongoDB - How to start in .NET
BsonClassMap.RegisterClassMap<Restaurant>(opt =>
{
opt.AutoMap();
opt.GetMemberMap(c => c.RestaurantId).SetElementName("restaurant_id");
opt.GetMemberMap(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId));
opt.SetIgnoreExtraElements(true);
});