Skip to content

Commit eee72f2

Browse files
committed
successfully inserted data to MongoDB database using console application
1 parent 37cb813 commit eee72f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+358
-102
lines changed

cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/Program.cs

Lines changed: 19 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -6,110 +6,35 @@
66

77
namespace DotnetCore.MongoDB
88
{
9+
// Just Declare the Field for the Student Collection
910
public class Students
1011
{
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;}
12+
public ObjectId Id { get; set; }
13+
public string Firstname { get; set; }
14+
public string Lastname { get; set; }
15+
public string City { get; set; }
16+
public string Age { get; set; }
1617
}
1718

18-
public class Program
19+
class Program
1920
{
20-
protected static IMangoClient _client;
21-
protected static IMongoDatabase _database;
22-
23-
public static Students GetStudent()
21+
public static void Main(string[] args)
2422
{
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,
23+
var client = new MongoClient("mongodb://localhost:27017");
24+
var database = client.GetDatabase("Student");
25+
var collection = database.GetCollection<BsonDocument>("student");
26+
27+
var document = new BsonDocument{
28+
{"Firstname", "Julhas2"},
29+
{"Lastname", "Hossain2"},
30+
{"City", "London"},
31+
{"Age", "26"}
4332
};
4433

45-
return student;
46-
}
34+
collection.InsertOneAsync(document);
35+
Console.Read();
4736

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();
5737
}
5838

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-
}
11439
}
11540
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
{
2+
"runtimeTarget": {
3+
"name": ".NETCoreApp,Version=v5.0",
4+
"signature": ""
5+
},
6+
"compilationOptions": {},
7+
"targets": {
8+
".NETCoreApp,Version=v5.0": {
9+
"DotnetCore.MongoDB/1.0.0": {
10+
"dependencies": {
11+
"MongoDB.Driver": "2.13.1"
12+
},
13+
"runtime": {
14+
"DotnetCore.MongoDB.dll": {}
15+
}
16+
},
17+
"DnsClient/1.4.0": {
18+
"runtime": {
19+
"lib/netstandard2.1/DnsClient.dll": {
20+
"assemblyVersion": "1.4.0.0",
21+
"fileVersion": "1.4.0.0"
22+
}
23+
}
24+
},
25+
"Microsoft.NETCore.Platforms/2.1.2": {},
26+
"MongoDB.Bson/2.13.1": {
27+
"dependencies": {
28+
"System.Runtime.CompilerServices.Unsafe": "5.0.0"
29+
},
30+
"runtime": {
31+
"lib/netstandard2.1/MongoDB.Bson.dll": {
32+
"assemblyVersion": "2.13.1.0",
33+
"fileVersion": "2.13.1.0"
34+
}
35+
}
36+
},
37+
"MongoDB.Driver/2.13.1": {
38+
"dependencies": {
39+
"MongoDB.Bson": "2.13.1",
40+
"MongoDB.Driver.Core": "2.13.1",
41+
"MongoDB.Libmongocrypt": "1.2.2"
42+
},
43+
"runtime": {
44+
"lib/netstandard2.1/MongoDB.Driver.dll": {
45+
"assemblyVersion": "2.13.1.0",
46+
"fileVersion": "2.13.1.0"
47+
}
48+
}
49+
},
50+
"MongoDB.Driver.Core/2.13.1": {
51+
"dependencies": {
52+
"DnsClient": "1.4.0",
53+
"MongoDB.Bson": "2.13.1",
54+
"MongoDB.Libmongocrypt": "1.2.2",
55+
"SharpCompress": "0.23.0",
56+
"System.Buffers": "4.5.1"
57+
},
58+
"runtime": {
59+
"lib/netstandard2.1/MongoDB.Driver.Core.dll": {
60+
"assemblyVersion": "2.13.1.0",
61+
"fileVersion": "2.13.1.0"
62+
}
63+
},
64+
"runtimeTargets": {
65+
"runtimes/win/native/libzstd.dll": {
66+
"rid": "win",
67+
"assetType": "native",
68+
"fileVersion": "0.0.0.0"
69+
},
70+
"runtimes/win/native/snappy32.dll": {
71+
"rid": "win",
72+
"assetType": "native",
73+
"fileVersion": "0.0.0.0"
74+
},
75+
"runtimes/win/native/snappy64.dll": {
76+
"rid": "win",
77+
"assetType": "native",
78+
"fileVersion": "0.0.0.0"
79+
}
80+
}
81+
},
82+
"MongoDB.Libmongocrypt/1.2.2": {
83+
"runtime": {
84+
"lib/netstandard2.1/MongoDB.Libmongocrypt.dll": {
85+
"assemblyVersion": "1.2.2.0",
86+
"fileVersion": "1.2.2.0"
87+
}
88+
},
89+
"runtimeTargets": {
90+
"runtimes/linux/native/libmongocrypt.so": {
91+
"rid": "linux",
92+
"assetType": "native",
93+
"fileVersion": "0.0.0.0"
94+
},
95+
"runtimes/osx/native/libmongocrypt.dylib": {
96+
"rid": "osx",
97+
"assetType": "native",
98+
"fileVersion": "0.0.0.0"
99+
},
100+
"runtimes/win/native/mongocrypt.dll": {
101+
"rid": "win",
102+
"assetType": "native",
103+
"fileVersion": "0.0.0.0"
104+
}
105+
}
106+
},
107+
"SharpCompress/0.23.0": {
108+
"dependencies": {
109+
"System.Text.Encoding.CodePages": "4.5.1"
110+
},
111+
"runtime": {
112+
"lib/netstandard2.0/SharpCompress.dll": {
113+
"assemblyVersion": "0.23.0.0",
114+
"fileVersion": "0.23.0.0"
115+
}
116+
}
117+
},
118+
"System.Buffers/4.5.1": {},
119+
"System.Runtime.CompilerServices.Unsafe/5.0.0": {},
120+
"System.Text.Encoding.CodePages/4.5.1": {
121+
"dependencies": {
122+
"Microsoft.NETCore.Platforms": "2.1.2",
123+
"System.Runtime.CompilerServices.Unsafe": "5.0.0"
124+
}
125+
}
126+
}
127+
},
128+
"libraries": {
129+
"DotnetCore.MongoDB/1.0.0": {
130+
"type": "project",
131+
"serviceable": false,
132+
"sha512": ""
133+
},
134+
"DnsClient/1.4.0": {
135+
"type": "package",
136+
"serviceable": true,
137+
"sha512": "sha512-CO1NG1zQdV0nEAXmr/KppLZ0S1qkaPwV0kPX5YPgmYBtrBVh1XMYHM54IXy3RBJu1k4thFtpzwo4HNHqxiuFYw==",
138+
"path": "dnsclient/1.4.0",
139+
"hashPath": "dnsclient.1.4.0.nupkg.sha512"
140+
},
141+
"Microsoft.NETCore.Platforms/2.1.2": {
142+
"type": "package",
143+
"serviceable": true,
144+
"sha512": "sha512-mOJy3M0UN+LUG21dLGMxaWZEP6xYpQEpLuvuEQBaownaX4YuhH6NmNUlN9si+vNkAS6dwJ//N1O4DmLf2CikVg==",
145+
"path": "microsoft.netcore.platforms/2.1.2",
146+
"hashPath": "microsoft.netcore.platforms.2.1.2.nupkg.sha512"
147+
},
148+
"MongoDB.Bson/2.13.1": {
149+
"type": "package",
150+
"serviceable": true,
151+
"sha512": "sha512-B/AI2we7YwfFuNcXr4jlCnVI5dJp+g8vkyj3Shi/bOpGhZQaxW4jK/NiiTwYvqxaMOe4MtVR+6Zlk4D2zalC7A==",
152+
"path": "mongodb.bson/2.13.1",
153+
"hashPath": "mongodb.bson.2.13.1.nupkg.sha512"
154+
},
155+
"MongoDB.Driver/2.13.1": {
156+
"type": "package",
157+
"serviceable": true,
158+
"sha512": "sha512-BGpE1Ce33T3MrStgxC4CPLVDA2ngsI+WZk4SYn+ycOaU5uQ7zwbQzqpdzHrS7EcMNVpD9r2wnpjos0nxhxvshw==",
159+
"path": "mongodb.driver/2.13.1",
160+
"hashPath": "mongodb.driver.2.13.1.nupkg.sha512"
161+
},
162+
"MongoDB.Driver.Core/2.13.1": {
163+
"type": "package",
164+
"serviceable": true,
165+
"sha512": "sha512-lLYYMBneLHA9PYwWzbfC3p0NkAqzxbT8ZuinCH9MwR2bgHqu/6cOaJG12twKsnjc6KJVBboY0ufjHt43T9ZLnA==",
166+
"path": "mongodb.driver.core/2.13.1",
167+
"hashPath": "mongodb.driver.core.2.13.1.nupkg.sha512"
168+
},
169+
"MongoDB.Libmongocrypt/1.2.2": {
170+
"type": "package",
171+
"serviceable": true,
172+
"sha512": "sha512-pKrlKJk4wem4MyfkaMqj+sqJ+RwUDnqnt76/Xwm5DDjMzEU5QtDkzQOLq5bVwFhNgC8LMn6Hr22tl5WsmN5+AA==",
173+
"path": "mongodb.libmongocrypt/1.2.2",
174+
"hashPath": "mongodb.libmongocrypt.1.2.2.nupkg.sha512"
175+
},
176+
"SharpCompress/0.23.0": {
177+
"type": "package",
178+
"serviceable": true,
179+
"sha512": "sha512-HBbT47JHvNrsZX2dTBzUBOSzBt+EmIRGLIBkbxcP6Jef7DB4eFWQX5iHWV3Nj7hABFPCjISrZ8s0z72nF2zFHQ==",
180+
"path": "sharpcompress/0.23.0",
181+
"hashPath": "sharpcompress.0.23.0.nupkg.sha512"
182+
},
183+
"System.Buffers/4.5.1": {
184+
"type": "package",
185+
"serviceable": true,
186+
"sha512": "sha512-Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg==",
187+
"path": "system.buffers/4.5.1",
188+
"hashPath": "system.buffers.4.5.1.nupkg.sha512"
189+
},
190+
"System.Runtime.CompilerServices.Unsafe/5.0.0": {
191+
"type": "package",
192+
"serviceable": true,
193+
"sha512": "sha512-ZD9TMpsmYJLrxbbmdvhwt9YEgG5WntEnZ/d1eH8JBX9LBp+Ju8BSBhUGbZMNVHHomWo2KVImJhTDl2hIgw/6MA==",
194+
"path": "system.runtime.compilerservices.unsafe/5.0.0",
195+
"hashPath": "system.runtime.compilerservices.unsafe.5.0.0.nupkg.sha512"
196+
},
197+
"System.Text.Encoding.CodePages/4.5.1": {
198+
"type": "package",
199+
"serviceable": true,
200+
"sha512": "sha512-4J2JQXbftjPMppIHJ7IC+VXQ9XfEagN92vZZNoG12i+zReYlim5dMoXFC1Zzg7tsnKDM7JPo5bYfFK4Jheq44w==",
201+
"path": "system.text.encoding.codepages/4.5.1",
202+
"hashPath": "system.text.encoding.codepages.4.5.1.nupkg.sha512"
203+
}
204+
}
205+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"runtimeOptions": {
3+
"additionalProbingPaths": [
4+
"/home/julhas/.dotnet/store/|arch|/|tfm|",
5+
"/home/julhas/.nuget/packages"
6+
]
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"runtimeOptions": {
3+
"tfm": "net5.0",
4+
"framework": {
5+
"name": "Microsoft.NETCore.App",
6+
"version": "5.0.0"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// <autogenerated />
2+
using System;
3+
using System.Reflection;
4+
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// Runtime Version:4.0.30319.42000
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
using System;
12+
using System.Reflection;
13+
14+
[assembly: System.Reflection.AssemblyCompanyAttribute("DotnetCore.MongoDB")]
15+
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
16+
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
17+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
18+
[assembly: System.Reflection.AssemblyProductAttribute("DotnetCore.MongoDB")]
19+
[assembly: System.Reflection.AssemblyTitleAttribute("DotnetCore.MongoDB")]
20+
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
21+
22+
// Generated by the MSBuild WriteCodeFragment class.
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
da5a3588e94ec011fefc7eac80b399182180e09e
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
is_global = true
2+
build_property.TargetFramework = net5.0
3+
build_property.TargetPlatformMinVersion =
4+
build_property.UsingMicrosoftNETSdkWeb =
5+
build_property.ProjectTypeGuids =
6+
build_property.PublishSingleFile =
7+
build_property.IncludeAllContentForSelfExtract =
8+
build_property._SupportedPlatformList = Android,iOS,Linux,macOS,Windows
9+
build_property.RootNamespace = DotnetCore.MongoDB
10+
build_property.ProjectDir = /home/julhas/Desktop/aspdotnetcore-development-strategies/cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/

cqrs-event-sourcing-rabbitmq-mongodb/DotnetCore.MongoDB/obj/Debug/net5.0/DotnetCore.MongoDB.csproj.CopyComplete

Whitespace-only changes.

0 commit comments

Comments
 (0)