Created
January 5, 2017 22:37
-
-
Save kevinadi/f0c8665e4e42b8855a2eb1bff7ee2b18 to your computer and use it in GitHub Desktop.
Small example of using MongoDB dotnet driver (dotnet-core, MongoDB.Driver 2.4.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using MongoDB.Bson; | |
using MongoDB.Driver; | |
namespace ConsoleApplication | |
{ | |
public class Person { | |
public ObjectId Id { get; set; } | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public override string ToString() { | |
return "Person Name: " + Name + ", Age: " + Age; | |
} | |
} | |
public class MongoDotnetDriver | |
{ | |
public static void ListDatabases(MongoClient client) { | |
Console.WriteLine("\n=== Databases ==="); | |
foreach(var db in client.ListDatabases().ToEnumerable()) { | |
Console.WriteLine(" - " + db); | |
} | |
} | |
public static void ListCollections(MongoClient client) { | |
Console.WriteLine("\n=== Collections ==="); | |
var database = client.GetDatabase("test"); | |
foreach(var coll in database.ListCollections().ToEnumerable()) { | |
Console.WriteLine(" - " + coll); | |
} | |
} | |
public static void InsertDocuments(MongoClient client) { | |
Console.WriteLine("\n=== Insert documents ==="); | |
var database = client.GetDatabase("test"); | |
var collection = database.GetCollection<BsonDocument>("dotnet"); | |
/* Drop collection */ | |
database.DropCollection("dotnet"); | |
/* Insert one document */ | |
var document = new BsonDocument | |
{ | |
{"name", "MongoDB"}, | |
{"type", "database"}, | |
{"info", new BsonDocument | |
{{"x", 123}, {"y", 456}} | |
} | |
}; | |
Console.WriteLine("> " + document); | |
collection.InsertOne(document); | |
/* Insert many documents */ | |
var documents = Enumerable.Range(0,5).Select(i => new BsonDocument("a", i)); | |
foreach(var row in documents) { | |
Console.WriteLine("> " + row); | |
} | |
collection.InsertMany(documents); | |
} | |
public static void FindDocuments(IMongoCollection<BsonDocument> collection) { | |
Console.WriteLine("\n=== Find ==="); | |
/* Query all documents */ | |
Console.WriteLine("--- all ---"); | |
var docs = collection.Find(new BsonDocument()).ToEnumerable(); | |
foreach(var doc in docs) { | |
Console.WriteLine("<< " + doc); | |
} | |
/* Query with filter */ | |
Console.WriteLine("--- with filter ---"); | |
var param = BsonDocument.Parse("{a: {$gt: 2}}"); | |
Console.WriteLine(param); | |
foreach(var doc in collection.Find(param).ToEnumerable()) { | |
Console.WriteLine("<< " + doc); | |
} | |
} | |
public static void AggregateDocuments(IMongoCollection<BsonDocument> collection) { | |
Console.WriteLine("\n=== Aggregate ==="); | |
var agg = collection.Aggregate() | |
.Match(new BsonDocument { {"a", new BsonDocument {{"$gt", 0}}} }) | |
.Group(BsonDocument.Parse("{_id: null, total: {$sum: '$a'}, avg: {$avg: '$a'}}")); | |
foreach(var doc in agg.ToEnumerable()) { | |
Console.WriteLine(doc); | |
} | |
} | |
public static void InsertObject(MongoClient client) { | |
Console.WriteLine("\n=== Insert object ==="); | |
var database = client.GetDatabase("test"); | |
var collection = database.GetCollection<Person>("Persons"); | |
database.DropCollection("Persons"); | |
var person1 = new Person { Name = "John Rambo", Age = 30 }; | |
var person2 = new Person { Name = "Stephen Seagal", Age = 40 }; | |
var persons = new [] { | |
new Person { Name = "Jet Li", Age = 20 }, | |
new Person { Name = "Chuck Norris", Age = 31 }, | |
new Person { Name = "John McClane", Age = 25 } | |
}; | |
collection.InsertOne(person1); | |
collection.InsertOne(person2); | |
collection.InsertMany(persons); | |
Console.WriteLine("--- All persons ---"); | |
foreach(var person in collection.Find(new BsonDocument()).ToEnumerable()) { | |
Console.WriteLine("<< " + person); | |
} | |
Console.WriteLine("--- 30 and older ---"); | |
foreach(var person in collection.Find(BsonDocument.Parse("{Age: {$gte: 30}}")).ToEnumerable()) { | |
Console.WriteLine("<< " + person); | |
} | |
Console.WriteLine("--- 30 and older (LINQ) ---"); | |
var query1 = from person in collection.AsQueryable() | |
where person.Age >= 30 | |
select person; | |
foreach(var p in query1.AsEnumerable()) { | |
Console.WriteLine(p); | |
} | |
var query2 = collection.AsQueryable().Where(p => p.Age >= 30).Select(p => p.Age); | |
Console.WriteLine("Aggregation: " + query2); | |
Console.WriteLine("Total age: " + query2.Sum()); | |
Console.WriteLine("Avg age: " + query2.Average()); | |
} | |
public static void Main(string[] args) | |
{ | |
/* Connection */ | |
var client = new MongoClient("mongodb://localhost:27017"); | |
var database = client.GetDatabase("test"); | |
var collection = database.GetCollection<BsonDocument>("dotnet"); | |
MongoDotnetDriver.ListDatabases(client); | |
MongoDotnetDriver.ListCollections(client); | |
MongoDotnetDriver.InsertDocuments(client); | |
MongoDotnetDriver.FindDocuments(collection); | |
MongoDotnetDriver.AggregateDocuments(collection); | |
MongoDotnetDriver.InsertObject(client); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment