static void Main(string[] args) { Console.WriteLine("What would you like to enter for MyDatas?"); // Get some input from user var myDatas = Console.ReadLine(); // Create an object with the data that was entered var obj = new MyHelloWorldMongoThing() { MyDatas = myDatas }; // get a mongoclient using the default connection string var mongo = new MongoClient(); // get (and create if doesn't exist) a database from the mongoclient var db = mongo.GetDatabase("MyHelloWorldDb"); // get a collection of MyHelloWorldMongoThings (and create if it doesn't exist) var collection = db.GetCollection <MyHelloWorldMongoThing>("myHelloWorldCollection"); // Count the items in the collection prior to insert // Using an empty filter so that everything is considered in the filter. var count = collection.CountDocuments(new FilterDefinitionBuilder <MyHelloWorldMongoThing>().Empty); Console.WriteLine($"Number of items in the collection before insert: {count}"); // Add the entered item to the collection collection.InsertOne(obj); // Count the items in the collection post insert count = collection.CountDocuments(new FilterDefinitionBuilder <MyHelloWorldMongoThing>().Empty); Console.WriteLine($"Number of items in the collection after insert: {count}"); }
static async Task Main(string[] args) { int n = 10; for (int i = 0; i < n; i++) { Console.WriteLine("What would you like to enter client for MyDatas?"); //получить ввод от пользователя var myDatas = Console.ReadLine(); //создать объект с данными, которые были введены var obj = new MyHelloWorldMongoThing() { MyDatas = myDatas }; //получить монгоклинета, используя строку подключения по умолчанию var mongo = new MongoClient(); //получить(и создать, если не существует) базу данных из монгоклиента var db = mongo.GetDatabase("MyHelloWorldDb"); //получить коллекцию MyHelloWorldMongoThing(и создать, если она не существует) //Использование пустого фильтра, чтобы все учитывалось в фильтре var collection = db.GetCollection <MyHelloWorldMongoThing>("myHelloWorldCollection"); //подсчитать предметы в коллекции до вставки var count = collection.CountDocuments(new FilterDefinitionBuilder <MyHelloWorldMongoThing>().Empty); //добавить введенный элемент в коллекцию collection.InsertOne(obj); //подсчитать предметы в коллекции после вставки count = collection.CountDocuments(new FilterDefinitionBuilder <MyHelloWorldMongoThing>().Empty); Console.WriteLine("Received data from MongoDB: "); await GetMessages(); var messages = GetMessages().Result; foreach (var message in messages) { Console.WriteLine(message.MyDatas); } } Console.ReadKey(); }