Skip to content

Commit 67b28aa

Browse files
committed
MongoDb Getting started
1 parent c909be6 commit 67b28aa

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Kritner.MongoDb.GettingStarted.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.28307.136
55
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3E4B86C9-73AD-42CD-8C82-6A5886850A4E}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kritner.MongoDb.GettingStarted.ConsoleApp", "src\Kritner.MongoDb.GettingStarted.ConsoleApp\Kritner.MongoDb.GettingStarted.ConsoleApp.csproj", "{593CDB5C-6BC3-4FBF-BA47-50958C9DB513}"
9+
EndProject
610
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{593CDB5C-6BC3-4FBF-BA47-50958C9DB513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{593CDB5C-6BC3-4FBF-BA47-50958C9DB513}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{593CDB5C-6BC3-4FBF-BA47-50958C9DB513}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{593CDB5C-6BC3-4FBF-BA47-50958C9DB513}.Release|Any CPU.Build.0 = Release|Any CPU
20+
EndGlobalSection
721
GlobalSection(SolutionProperties) = preSolution
822
HideSolutionNode = FALSE
923
EndGlobalSection
24+
GlobalSection(NestedProjects) = preSolution
25+
{593CDB5C-6BC3-4FBF-BA47-50958C9DB513} = {3E4B86C9-73AD-42CD-8C82-6A5886850A4E}
26+
EndGlobalSection
1027
GlobalSection(ExtensibilityGlobals) = postSolution
1128
SolutionGuid = {47461D79-65F5-4AC2-B9EE-75A8F6D374B7}
1229
EndGlobalSection
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MongoDB.Driver" Version="2.7.3" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using MongoDB.Driver;
2+
using System;
3+
4+
namespace Kritner.MongoDb.GettingStarted.ConsoleApp
5+
{
6+
public class MyHelloWorldMongoThing
7+
{
8+
public DateTime DateCreated { get; set; } = DateTime.Now;
9+
public string MyDatas { get; set; }
10+
}
11+
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
Console.WriteLine("What would you like to enter for MyDatas?");
17+
18+
// Get some input from user
19+
var myDatas = Console.ReadLine();
20+
21+
// Create an object with the data that was entered
22+
var obj = new MyHelloWorldMongoThing()
23+
{
24+
MyDatas = myDatas
25+
};
26+
27+
// get a mongoclient using the default connection string
28+
var mongo = new MongoClient();
29+
30+
// get (and create if doesn't exist) a database from the mongoclient
31+
var db = mongo.GetDatabase("MyHelloWorldDb");
32+
33+
// get a collection of MyHelloWorldMongoThings (and create if it doesn't exist)
34+
var collection = db.GetCollection<MyHelloWorldMongoThing>("myHelloWorldCollection");
35+
36+
// Count the items in the collection prior to insert
37+
// Using an empty filter so that everything is considered in the filter.
38+
var count = collection.CountDocuments(new FilterDefinitionBuilder<MyHelloWorldMongoThing>().Empty);
39+
Console.WriteLine($"Number of items in the collection before insert: {count}");
40+
41+
// Add the entered item to the collection
42+
collection.InsertOne(obj);
43+
44+
// Count the items in the collection post insert
45+
count = collection.CountDocuments(new FilterDefinitionBuilder<MyHelloWorldMongoThing>().Empty);
46+
Console.WriteLine($"Number of items in the collection after insert: {count}");
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)