MongoDB探索之路(二)——系统设计之CRUD
1.构造实体类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace EM.Model 8 { 9 public class News 10 { 11 public string _id { get; set; } 12 public string title { get; set; } 13 public string url { get; set; } 14 public string author { get; set; } 15 public int vote_count { get; set; } 16 public List<string> tags { get; set; } 17 public image image { get; set; } 18 public List<comments> comments { get; set; } 19 public DateTime createTime { get; set; } 20 public DateTime updateTime { get; set; } 21 } 22 public class image 23 { 24 public string url { get; set; } 25 public string caption { get; set; } 26 public string type { get; set; } 27 public string size { get; set; } 28 public string data { get; set; } 29 } 30 public class comments 31 { 32 public int userId { get; set; } 33 public string user { get; set; } 34 public string text { get; set; } 35 public DateTime createTime { get; set; } 36 public DateTime updateTime { get; set; } 37 } 38 39 40 }
2.增加一条记录
1 News news = new News(); 2 news._id = Guid.NewGuid().ToString(); 3 news.title = "大新闻"; 4 news.url = "http://www.cnblogs.com/cnki/"; 5 news.author = "沐风"; 6 news.vote_count = 100; 7 List<string> tagsList = new List<string>() { "国际", "编程", "dota" }; 8 news.tags = tagsList; 9 image img = new image(); 10 img.url = "http://www.cnblogs.com/cnki/"; 11 img.caption = "好图"; 12 img.type = "回忆录"; 13 img.size = "3M"; 14 img.data = "dota三人组"; 15 news.image = img; 16 17 List<comments> commentsList = new List<EM.Model.comments>(); 18 comments comment1 = new comments(); 19 comment1.userId = 1; 20 comment1.user = "三生石"; 21 comment1.text = "你所有的努力,只是为了更好的衬托别人的成功。"; 22 comment1.createTime = DateTime.Now; 23 comment1.updateTime = DateTime.Now; 24 commentsList.Add(comment1); 25 26 comments comment2 = new comments(); 27 comment2.userId = 2; 28 comment2.user = "影魔"; 29 comment2.text = "明明可以靠脸吃饭,他却偏要靠才华,最后还是饿死了。"; 30 comment2.createTime = DateTime.Now; 31 comment2.updateTime = DateTime.Now; 32 commentsList.Add(comment2); 33 34 35 news.comments = commentsList; 36 37 news.createTime = DateTime.Now; 38 news.updateTime = DateTime.Now; 39 40 bool b = MongoDBServiceFactory.CreateMongoDBService(conStrNo, databaseName).Insert<News>("News", news);
存入MongoDB后的单条集合格式
{ "_id" : "95565797-8572-4281-b6e6-6820d0c8dc37", "title" : "大新闻", "url" : "http://www.cnblogs.com/cnki/", "author" : "沐风", "vote_count" : 100, "tags" : [ "国际", "编程", "dota" ], "image" : { "url" : "http://www.cnblogs.com/cnki/", "caption" : "好图", "type" : "回忆录", "size" : "3M", "data" : "dota三人组" }, "comments" : [ { "userId" : 1, "user" : "三生石", "text" : "你所有的努力,只是为了更好的衬托别人的成功。", "createTime" : ISODate("2016-12-05T13:40:46.142Z"), "updateTime" : ISODate("2016-12-05T13:40:46.142Z") }, { "userId" : 2, "user" : "影魔", "text" : "明明可以靠脸吃饭,他却偏要靠才华,最后还是饿死了。", "createTime" : ISODate("2016-12-05T13:40:46.142Z"), "updateTime" : ISODate("2016-12-05T13:40:46.142Z") } ], "createTime" : ISODate("2016-12-05T13:40:46.142Z"), "updateTime" : ISODate("2016-12-05T13:40:46.142Z") }
增加一条评论
1 comments commentsInfo = new comments(); 2 commentsInfo.userId = 3; 3 commentsInfo.user = "mf"; 4 commentsInfo.text = "世界太美好"; 5 commentsInfo.createTime = DateTime.Now; 6 commentsInfo.updateTime = DateTime.Now; 7 var update = Builders<News>.Update.Push(m => m.comments, commentsInfo); 8 bool bUpdateOne = MongoDBServiceFactory.CreateMongoDBService(conStrNo, databaseName).UpdateOne<News>("News", m => m._id == "95565797-8572-4281-b6e6-6820d0c8dc37", update);
结果
1 { 2 "_id" : "95565797-8572-4281-b6e6-6820d0c8dc37", 3 "title" : "大新闻", 4 "url" : "http://www.cnblogs.com/cnki/", 5 "author" : "沐风", 6 "vote_count" : 100, 7 "tags" : [ 8 "国际", 9 "编程", 10 "dota" 11 ], 12 "image" : { 13 "url" : "http://www.cnblogs.com/cnki/", 14 "caption" : "好图", 15 "type" : "回忆录", 16 "size" : "3M", 17 "data" : "dota三人组" 18 }, 19 "comments" : [ 20 { 21 "userId" : 1, 22 "user" : "三生石", 23 "text" : "你所有的努力,只是为了更好的衬托别人的成功。", 24 "createTime" : ISODate("2016-12-05T13:40:46.142Z"), 25 "updateTime" : ISODate("2016-12-05T13:40:46.142Z") 26 }, 27 { 28 "userId" : 2, 29 "user" : "影魔", 30 "text" : "明明可以靠脸吃饭,他却偏要靠才华,最后还是饿死了。", 31 "createTime" : ISODate("2016-12-05T13:40:46.142Z"), 32 "updateTime" : ISODate("2016-12-05T13:40:46.142Z") 33 }, 34 { 35 "userId" : 3, 36 "user" : "mf", 37 "text" : "世界太美好", 38 "createTime" : ISODate("2016-12-05T13:44:16.831Z"), 39 "updateTime" : ISODate("2016-12-05T13:44:16.832Z") 40 } 41 ], 42 "createTime" : ISODate("2016-12-05T13:40:46.142Z"), 43 "updateTime" : ISODate("2016-12-05T13:40:46.142Z") 44 }
3.查询一条记录
News news = MongoDBServiceFactory.CreateMongoDBService(conStr, databaseName).Single<News>("News", m => m.author == "沐风");
4.更新一条记录
a.支持数增加1
var updateDef = Builders<News>.Update.Inc(x => x.vote_count, 1); bool update = MongoDBServiceFactory.CreateMongoDBService(conStr, databaseName).UpdateOne<News>("News", x => x._id == "95565797-8572-4281-b6e6-6820d0c8dc37", updateDef);
b.修改Title字段
var update = Builders<News>.Update.Set(m => m.title, "好大的新闻"); bool updateResult = MongoDBServiceFactory.CreateMongoDBService(conStr, databaseName).UpdateOne("News", x => x._id == "95565797-8572-4281-b6e6-6820d0c8dc37", update);
c.根据userId更新一条子评论的comments字段
1 //找到子文档 2 var a = Builders<News>.Filter.Eq(x => x._id, "95565797-8572-4281-b6e6-6820d0c8dc37"); 3 var b = Builders<News>.Filter.ElemMatch(x => x.comments, y => y.userId == 1); 4 var filter = Builders<News>.Filter.And(new FilterDefinition<News>[] { a, b }); 5 //更新子文档的字段 6 var field = new StringFieldDefinition<News, string>("comments.$.comments"); 7 var update = Builders<News>.Update.Set(field, "修改后的我"); 8 //更新库 9 UpdateResult updateResult = MongoDBServiceFactory.CreateMongoDBService(conStrNo, databaseName).DocumentUpdate("News", filter, update); 10 bool bupdateResult= updateResult != null && updateResult.ModifiedCount > 0 && updateResult.ModifiedCount == updateResult.MatchedCount ? true : false;
5.删除
bool delete = MongoDBServiceFactory.CreateMongoDBService(conStr, databaseName).Delete<News>("News", x => x._id == "95565797-8572-4281-b6e6-6820d0c8dc37") > 0;