MongoDB增删查改

Posted DaivdHu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);        }

以上是关于MongoDB增删查改的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB增删查改

mongodb(四):对文档操作增删查改(python)

MongoDB数据库进阶 --- 增删查改...

node.js+express+mongoose实现用户增删查改案例

2016/3/13 MySQL 增删查改 CRUD 用代码实现

php mysql增删查改