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

相关推荐