• MongoDB增、删、查、改


    static MongoClient client = new MongoClient("mongodb://localhost:27017");
    
            static IMongoDatabase database = client.GetDatabase("Student");
    
            static void Main(string[] args)
            {
    
                // To directly connect to a single MongoDB server
                // (this will not auto-discover the primary even if it's a member of a replica set)
                //var client = new MongoClient();
    
                // or use a connection string
    
    
    
    
                // or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
                //var client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019");
    
                //MongoDBInsert();
                //MongoDBUpdate();
                //MongoDBDelete();
                MongoDBList();
                Console.WriteLine("End...");
    
                Console.ReadKey();
    
            }
    
    
    
    
    
    
            public static void MongoDBFind()
            {
    
    
                //过滤器
                //var filter = Builders<BsonDocument>.Filter.Eq("i", 71);
                var collection = database.GetCollection<BsonDocument>("test");
    
    
                var document = collection.Find(new BsonDocument()).FirstOrDefault();
    
                Console.WriteLine(document.ToString());
            }
    
    
            public static void MongoDBList()
            {
    
                //过滤器
                //var filter = Builders<BsonDocument>.Filter.Eq("i", 71);
                var collection = database.GetCollection<BsonDocument>("test");
    
                var document = collection.Find(new BsonDocument()).ToList();
                foreach (var item in document)
                {
                    Console.WriteLine(item.ToString());
                }
    
                
            }
    
    
            
            public static void MongoDBInsert()
            {
                var collection = database.GetCollection<BsonDocument>("test");
                BsonDocument document = new BsonDocument
                {
                    { "title", "test2" },
                    { "by", "mrbin2" },
                    { "content", "I'm is Mrbin" }
                };
                collection.InsertOne(document);
            }
            public static void MongoDBUpdate()
            {
                var collection = database.GetCollection<BsonDocument>("test");
                var filter = Builders<BsonDocument>.Filter.Eq("title", "test2");
                var update = Builders<BsonDocument>.Update.Set("content", "this is my power");
                collection.UpdateOne(filter, update);
                //await collection.UpdateOneAsync(filter, update);
            }
    
    
            public static void MongoDBDelete()
            {
                var collection = database.GetCollection<BsonDocument>("test");
                var filter = Builders<BsonDocument>.Filter.Eq("title", "test2");
                collection.DeleteOne(filter);
                //await collection.DeleteOneAsync(filter);
            }

    static MongoClient client = new MongoClient("mongodb://localhost:27017");
            static IMongoDatabase database = client.GetDatabase("Student");
            static void Main(string[] args)        {
                // To directly connect to a single MongoDB server            // (this will not auto-discover the primary even if it's a member of a replica set)            //var client = new MongoClient();
                // or use a connection string



                // or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members            //var client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019");
                //MongoDBInsert();            //MongoDBUpdate();            //MongoDBDelete();            MongoDBList();            Console.WriteLine("End...");
                Console.ReadKey();
            }





            public static void MongoDBFind()        {

                //过滤器            //var filter = Builders<BsonDocument>.Filter.Eq("i", 71);            var collection = database.GetCollection<BsonDocument>("test");

                var document = collection.Find(new BsonDocument()).FirstOrDefault();
                Console.WriteLine(document.ToString());        }

            public static void MongoDBList()        {
                //过滤器            //var filter = Builders<BsonDocument>.Filter.Eq("i", 71);            var collection = database.GetCollection<BsonDocument>("test");
                var document = collection.Find(new BsonDocument()).ToList();            foreach (var item in document)            {                Console.WriteLine(item.ToString());            }
                        }

                    public static void MongoDBInsert()        {            var collection = database.GetCollection<BsonDocument>("test");            BsonDocument document = new BsonDocument            {                { "title", "test2" },                { "by", "mrbin2" },                { "content", "I'm is Mrbin" }            };            collection.InsertOne(document);        }        public static void MongoDBUpdate()        {            var collection = database.GetCollection<BsonDocument>("test");            var filter = Builders<BsonDocument>.Filter.Eq("title", "test2");            var update = Builders<BsonDocument>.Update.Set("content", "this is my power");            collection.UpdateOne(filter, update);            //await collection.UpdateOneAsync(filter, update);        }

            public static void MongoDBDelete()        {            var collection = database.GetCollection<BsonDocument>("test");            var filter = Builders<BsonDocument>.Filter.Eq("title", "test2");            collection.DeleteOne(filter);            //await collection.DeleteOneAsync(filter);        }

  • 相关阅读:
    1007 素数对猜想 (20 分)
    1005 继续(3n+1)猜想 (25 分)
    1002 写出这个数 (20 分)
    1001 害死人不偿命的(3n+1)猜想 (15 分)
    mysql常用操作
    mysql乱码问题
    mysql忘记root密码
    linux开机启动
    fedora 调整屏幕亮度
    Access denied for user 'root'@'localhost' (using password:YES) 解决方案
  • 原文地址:https://www.cnblogs.com/DavidHuAtIT/p/12205803.html
Copyright © 2020-2023  润新知