Skip to content

Commit 37cb813

Browse files
committed
added the another folder dotnet core and mongodb
1 parent 81b3141 commit 37cb813

File tree

9 files changed

+785
-0
lines changed

9 files changed

+785
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="MongoDB.Driver" Version="2.13.1" />
9+
<Content Include="Program.cs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
3+
//MongoDB.Driver
4+
using MongoDB.Bson;
5+
using MongoDB.Driver;
6+
7+
namespace DotnetCore.MongoDB
8+
{
9+
public class Students
10+
{
11+
public ObjectId Id {get; set;}
12+
public string Firstname{get; set;}
13+
public string Lastname{get; set;}
14+
public string City {get; set;}
15+
public string Age {get; set;}
16+
}
17+
18+
public class Program
19+
{
20+
protected static IMangoClient _client;
21+
protected static IMongoDatabase _database;
22+
23+
public static Students GetStudent()
24+
{
25+
Console.WriteLine("Please enter studnet first name : ");
26+
string firstName = Console.ReadLine();
27+
28+
Console.WriteLine("Please enter student last name : ");
29+
string lastName = Console.ReadLine();
30+
31+
Console.WriteLine("Please enter student age : ");
32+
string StudentAge = console.ReadLine();
33+
34+
Console.WriteLine("Please enter city name : ");
35+
string StudentCity = console.ReadLine();
36+
37+
Students student = new Students()
38+
{
39+
Firstname = firstName,
40+
Lastname = lastName,
41+
Age = StudentAge,
42+
City = StudentCity,
43+
};
44+
45+
return student;
46+
}
47+
48+
public static void Main(string[] args)
49+
{
50+
Program p = new Program();
51+
p.CRUDwithMongoDb();
52+
53+
54+
//Hold the screen by logic
55+
Console.WriteLine("Press any key to trminated the program");
56+
Console.ReadKey();
57+
}
58+
59+
public void CRUDwithMongoDb()
60+
{
61+
_client = new MongoClient();
62+
_database = _client.GetDatabase("School");
63+
var _collection = _database.GetCollection<Students>("StudentDetails");
64+
Console.WriteLine("Press select your option from the following\n1 - Insert\n2 - Update One Document\n3 - Delete\n4 - Read All\n");
65+
string userSelection = Console.ReadLine();
66+
67+
switch (userSelection)
68+
{
69+
case "1":
70+
//Insert logic
71+
_collection.InsertOne(GetStudent());
72+
break;
73+
74+
case "2":
75+
//Update logic
76+
_collection.FindOneAndUpdate<Students>(
77+
Builders<Students>.Filter.Eq("FirstName", obj1.FirstName),
78+
Builders<Students>.Filter.Eq("LastName", obj1.LastName).Set("City", obj1.City).Set("Age", obj1.Age));
79+
);
80+
break;
81+
82+
case "3":
83+
//Find and Delete
84+
Console.WriteLine("Please Enter the first name to delete the record(so called document) : ");
85+
var deletefirstName = Console.ReadLine();
86+
_collection.DeleteOne(s => s.FirstName == deletefirstName);
87+
break;
88+
89+
case "4":
90+
//Read all existing document
91+
var all = _collection.Find(new BsonDocument());
92+
Console.WriteLine();
93+
94+
foreach (var i in all.ToEnumerable())
95+
{
96+
Console.WriteLine(i.Id + " " + i.FirstName + "\t" + i.LastName + "\t" + i.Age + "\t" + i.City);
97+
}
98+
break;
99+
100+
default:
101+
Console.WriteLine("Please choose a correct option");
102+
break;
103+
}
104+
105+
//To continue with Program
106+
Console.WriteLine("\n--------------------------------------------------------------\nPress Y for continue...\n");
107+
string userChoice = Console.ReadLine();
108+
109+
if (userChoice == "Y" || userChoice == "y")
110+
{
111+
this.CRUDwithMongoDb();
112+
}
113+
}
114+
}
115+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"MongoSettings": {
3+
"ConnectionString": "mongodb://connection string",
4+
"DatabaseName": "Student"
5+
}
6+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"format": 1,
3+
"restore": {
4+
"/home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/DotnetCore.MongoDB.csproj": {}
5+
},
6+
"projects": {
7+
"/home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/DotnetCore.MongoDB.csproj": {
8+
"version": "1.0.0",
9+
"restore": {
10+
"projectUniqueName": "/home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/DotnetCore.MongoDB.csproj",
11+
"projectName": "DotnetCore.MongoDB",
12+
"projectPath": "/home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/DotnetCore.MongoDB.csproj",
13+
"packagesPath": "/home/julhas/.nuget/packages/",
14+
"outputPath": "/home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/obj/",
15+
"projectStyle": "PackageReference",
16+
"configFilePaths": [
17+
"/home/julhas/.nuget/NuGet/NuGet.Config"
18+
],
19+
"originalTargetFrameworks": [
20+
"net5.0"
21+
],
22+
"sources": {
23+
"https://api.nuget.org/v3/index.json": {}
24+
},
25+
"frameworks": {
26+
"net5.0": {
27+
"targetAlias": "net5.0",
28+
"projectReferences": {}
29+
}
30+
},
31+
"warningProperties": {
32+
"warnAsError": [
33+
"NU1605"
34+
]
35+
}
36+
},
37+
"frameworks": {
38+
"net5.0": {
39+
"targetAlias": "net5.0",
40+
"dependencies": {
41+
"MongoDB.Driver": {
42+
"target": "Package",
43+
"version": "[2.13.1, )"
44+
}
45+
},
46+
"imports": [
47+
"net461",
48+
"net462",
49+
"net47",
50+
"net471",
51+
"net472",
52+
"net48"
53+
],
54+
"assetTargetFallback": true,
55+
"warn": true,
56+
"frameworkReferences": {
57+
"Microsoft.NETCore.App": {
58+
"privateAssets": "all"
59+
}
60+
},
61+
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/5.0.401/RuntimeIdentifierGraph.json"
62+
}
63+
}
64+
}
65+
}
66+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
4+
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
5+
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
6+
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
7+
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/julhas/.nuget/packages/</NuGetPackageRoot>
8+
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/julhas/.nuget/packages/</NuGetPackageFolders>
9+
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
10+
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">5.11.0</NuGetToolVersion>
11+
</PropertyGroup>
12+
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
13+
<SourceRoot Include="/home/julhas/.nuget/packages/" />
14+
</ItemGroup>
15+
<PropertyGroup>
16+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
17+
</PropertyGroup>
18+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
2+
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
</PropertyGroup>
6+
</Project>

0 commit comments

Comments
 (0)