Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MongoDB.Driver;
- using System;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Linq;
- using MongoDB.Bson;
- using MongoDB.Driver.Core;
- namespace testdesfonctions
- {
- internal class Student
- {
- public ObjectId _id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Class { get; set; }
- public int Age { get; set; }
- public IEnumerable<string> Subjects { get; set; }
- }
- public class MongoCRUD
- {
- private IMongoDatabase db;
- public MongoCRUD(string database)
- { /*connecte à database ou cree la base du nom de database */
- var client = new MongoClient("mongodb://localhost");
- db = client.GetDatabase(database);
- }
- public void InsertRecord<T>(string table, T record)
- { /*ajouter un element record (sous forme ) dans une collection table, si table inexistant creation automatique */
- var collection = db.GetCollection<T>(table);
- collection.InsertOne(record);
- }
- public List<T> LoadRecords<T>(string table)
- { /*lire une collection entière*/
- var collection = db.GetCollection<T>(table);
- return collection.Find(new BsonDocument()).ToList();
- }
- public T LoadRecordById<T>(string table, ObjectId id)
- { /*chercher l'element de la collection table grace a son id */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq("_id", id);
- return collection.Find(filter).First();
- }
- public void UpsetRecord<T>(string table, ObjectId id, T record)
- { /*changer l'element id dans une collection par l'element record. Il est possible de remplacer une seule propriete de l'element */
- var collection = db.GetCollection<T>(table);
- var result = collection.ReplaceOne(
- new BsonDocument("_id", id),
- record,
- new UpdateOptions { IsUpsert = true });
- }
- public void DeleteRecord<T>(string table, ObjectId id)
- { /*supprimer un element identifie par son id */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq("_id", id);
- collection.DeleteOne(filter);
- }
- public List<T> LoadRecordByParameterString<T>(string table, string parameter, string parameterValue)
- { /*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode string) */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
- return collection.Find(filter).ToList();
- }
- public List<T> LoadRecordByParameterInt<T>(string table, string parameter, int parameterValue)
- {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode int) */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
- return collection.Find(filter).ToList();
- }
- public List<T> LoadRecordByParameterFloat<T>(string table, string parameter, float parameterValue)
- {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode float) */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
- return collection.Find(filter).ToList();
- }
- public List<T> LoadRecordByParameterBool<T>(string table, string parameter, bool parameterValue)
- {/*chercher l'element de la collection table grace a la valeur de l'un de ses parametres (mode bool) */
- var collection = db.GetCollection<T>(table);
- var filter = Builders<T>.Filter.Eq(parameter, parameterValue);
- return collection.Find(filter).ToList();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- MainAsync().Wait();
- Console.WriteLine("Press enter to exit");
- Console.ReadLine();
- }
- static async Task MainAsync()
- {
- /*connection a la base school */
- MongoCRUD db = new MongoCRUD("schoool");
- /*ajout d'un nouvel eleve */
- /*var studentAlexandre = new Student
- {
- _id = new ObjectId("5ce7fa420a7ff85b5154c2e5"),
- FirstName = "Alexandre",
- LastName = "Dupond",
- Subjects = new List<string> { "Elektronik", "Mathematik", "Phisik" },
- Class = "CSI 2",
- Age = 5
- };
- db.InsertRecord("students", studentAlexandre);*/
- /*changement d'informations Gregor (id 5ce7fa420a7ff85b5154c2e5) va changer de classe */
- ObjectId myId = new ObjectId("5ce7fa420a7ff85b5154c2e5");
- var oneRec = db.LoadRecordById<Student>("students", myId);
- oneRec.Class = "CSI 3";
- db.UpsetRecord<Student>("students", myId, oneRec);
- /*supprimons Alexandre (id 5ce7fa420a7ff85b5154c2e5)*/
- /*ObjectId myIdBis = new ObjectId("5ce7fa420a7ff85b5154c2e5");
- db.DeleteRecord<Student>("students", myIdBis);*/
- /*affichons la liste des etudiants en classe "JSS 3"*/
- var recs = db.LoadRecordByParameterString<Student>("students", "Class", "JSS 3");
- foreach (var rec in recs) {
- Console.WriteLine($"{rec._id}:{rec.FirstName}:{rec.LastName}:{rec.Class}:{rec.Age}"); }
- Console.WriteLine("\n");
- /*affichons la liste des etudiants qui ont "23" ans*/
- var rics = db.LoadRecordByParameterInt<Student>("students", "Age", 23);
- foreach (var ric in rics)
- {
- Console.WriteLine($"{ric._id}:{ric.FirstName}:{ric.LastName}:{ric.Class}:{ric.Age}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement