首页 > 数据库技术 > 详细

【MongoDB初识】-结合C#简单使用,驱动2.x

时间:2015-11-02 17:16:07      阅读:213      评论:0      收藏:0      [点我收藏+]
     public static Students GetEntityByName(string conStr, string userName = "bj")
        {
            Students s = new Students();
            MongoClient client = new MongoClient(conStr);
            var db = client.GetDatabase("test");
            var collection = db.GetCollection<Students>("students");
            var query = Builders<Students>.Filter.Eq("name", "hhe");
            s = collection.Find(query).FirstAsync().Result;
            return s;
        }
        public static List<Students> GetEntityList(string conStr)
        {
            List<Students> list = new List<Students>();
            MongoClient client = new MongoClient(conStr);
            var db = client.GetDatabase("test");
            var collection = db.GetCollection<Students>("students");
            list = collection.Find(a => a.age > 12).SortBy(a => a.age).ToListAsync().Result;
            return list;
        }

        public static bool UpdateEntityByName(string conStr, string userName = "bj")
        {
            bool s = false;
            MongoClient client = new MongoClient(conStr);
            var db = client.GetDatabase("test");
            var collection = db.GetCollection<Students>("students");
            var query = Builders<Students>.Filter.Eq("name", "hhe");
            var update = Builders<Students>.Update.Set(a => a.name, "hhee");
            //Builders<Student>.Update.AddToSetEach(s => s.CoursesList, courseList)
            var ss = collection.UpdateOneAsync(query, update).Result;
            if (ss.IsAcknowledged)
            {
                s = true;
            }
            return s;
        }
        public static async Task InsertEntity(string conStr)
        {
            Students s = new Students() { name = "www1", classid = 6, age = 26 };
            MongoClient client = new MongoClient(conStr);
            var db = client.GetDatabase("test");
            var collection = db.GetCollection<Students>("students");
            await collection.InsertOneAsync(s);
        }

 

【MongoDB初识】-结合C#简单使用,驱动2.x

原文:http://www.cnblogs.com/lb12081116/p/4930377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!