码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB增、删、查、改

时间:2020-01-17 15:12:07      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:wait   syn   oca   ring   foreach   dbi   doc   base   mem   

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);        }

MongoDB增、删、查、改

标签:wait   syn   oca   ring   foreach   dbi   doc   base   mem   

原文地址:https://www.cnblogs.com/DavidHuAtIT/p/12205803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!