【MongoDB初识】-结合C#简单使用,驱动2.x
2015-11-02 15:51
961 查看
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索引创建(5)
- 在批处理中调用JS操作MongoDB
- PHP安装MongoDB扩展
- MongoDB学习笔记(数据操作)
- java查询mongodb中的objectid示例(查询mongodb中的_id属性)
- Windows平台下安装MongoDB
- 在Ubuntu下手动安装配置Mongodb
- 8.nodejs权威指南--MongoDB
- mongodb查询
- Windows下安装mongoDB
- php操作mongodb教程
- MongoDB自学日记1——基本操作
- Linux非法关机后重新开启MongoDB
- php 操作mongodb
- zendframwork2添加对mongodb的支持
- 用CRT connect MongoDB 使用Backspace无效
- centos安装mongodb
- MongoDB高级查询详细
- 通过Python将MongoDB导出的json数据转换成Mysql的insert语句导入
- MongoDB创建系统管理员账户和数据库管理员账户