C# and MongoDB: Read Operation Quick Start
using System; | |
using MongoDB.Bson; | |
using MongoDB.Driver; | |
namespace MongoDBCRUDExample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MongoClient dbClient = new MongoClient("mongodb+srv://test_user:MongoDBR0cks@demodata-uymyo.mongodb.net/test?retryWrites=true&w=majority"); | |
var dbList = dbClient.ListDatabases().ToList(); | |
Console.WriteLine("The list of databases on this server is: "); | |
foreach (var db in dbList) | |
{ | |
Console.WriteLine(db); | |
} | |
Console.WriteLine("Connecting to sample_training.grades"); | |
var database = dbClient.GetDatabase("sample_training"); | |
var collection = database.GetCollection<BsonDocument>("grades"); | |
// Define a new student for the grade book. | |
var document = new BsonDocument | |
{ | |
{ "student_id", 10000 }, | |
{ "scores", new BsonArray | |
{ | |
new BsonDocument{ {"type", "exam"}, {"score", 88.12334193287023 } }, | |
new BsonDocument{ {"type", "quiz"}, {"score", 74.92381029342834 } }, | |
new BsonDocument{ {"type", "homework"}, {"score", 89.97929384290324 } }, | |
new BsonDocument{ {"type", "homework"}, {"score", 82.12931030513218 } } | |
} | |
}, | |
{ "class_id", 480} | |
}; | |
// ********************************* | |
// Create Operations | |
// ********************************* | |
// Insert the new student grade records into the database. | |
Console.WriteLine("Inserting Document"); | |
collection.InsertOne(document); | |
Console.WriteLine("Document Inserted.\n"); | |
// ********************************* | |
// Read Operations | |
// ********************************* | |
// Find first record in the database | |
var firstDocument = collection.Find(new BsonDocument()).FirstOrDefault(); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine(firstDocument.ToString()); | |
// Find a specific document with a filter | |
var filter = Builders<BsonDocument>.Filter.Eq("student_id", 10000); | |
var studentDocument = collection.Find(filter).FirstOrDefault(); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine(studentDocument.ToString()); | |
// Find all documents with an exam score equal or above 95 as a list | |
//var highExamScoreFilter = Builders<BsonDocument> | |
// .Filter.Eq("scores.type", "exam") & Builders<BsonDocument>.Filter.Gte("score", 95); | |
var highExamScoreFilter = Builders<BsonDocument>.Filter | |
.ElemMatch<BsonValue>("scores", | |
new BsonDocument { { "type", "exam" }, | |
{ "score", new BsonDocument { { "$gte", 95 } } } | |
}); | |
var highExamScores = collection.Find(highExamScoreFilter).ToList(); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine(highExamScores); | |
// Find all documents with an exam score equal | |
// or above 95 as an iterable | |
var cursor = collection.Find(highExamScoreFilter).ToCursor(); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine("\nHigh Scores Iterable\n"); | |
Console.WriteLine("\n**********\n"); | |
foreach (var cursorDocument in cursor.ToEnumerable()) | |
{ | |
Console.WriteLine(cursorDocument); | |
} | |
// Sort the exam scores by student_id | |
var sort = Builders<BsonDocument>.Sort.Descending("student_id"); | |
var highestScore = collection.Find(highExamScoreFilter).Sort(sort).First(); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine("\nHigh Score\n"); | |
Console.WriteLine("\n**********\n"); | |
Console.WriteLine(highestScore); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment