MongoDB探索之路(二)——系统设计之CRUD

时间:2023-03-09 03:15:24
MongoDB探索之路(二)——系统设计之CRUD

1.构造实体类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace EM.Model
{
public class News
{
public string _id { get; set; }
public string title { get; set; }
public string url { get; set; }
public string author { get; set; }
public int vote_count { get; set; }
public List<string> tags { get; set; }
public image image { get; set; }
public List<comments> comments { get; set; }
public DateTime createTime { get; set; }
public DateTime updateTime { get; set; }
}
public class image
{
public string url { get; set; }
public string caption { get; set; }
public string type { get; set; }
public string size { get; set; }
public string data { get; set; }
}
public class comments
{
public int userId { get; set; }
public string user { get; set; }
public string text { get; set; }
public DateTime createTime { get; set; }
public DateTime updateTime { get; set; }
} }

2.增加一条记录

             News news = new News();
news._id = Guid.NewGuid().ToString();
news.title = "大新闻";
news.url = "http://www.cnblogs.com/cnki/";
news.author = "沐风";
news.vote_count = ;
List<string> tagsList = new List<string>() { "国际", "编程", "dota" };
news.tags = tagsList;
image img = new image();
img.url = "http://www.cnblogs.com/cnki/";
img.caption = "好图";
img.type = "回忆录";
img.size = "3M";
img.data = "dota三人组";
news.image = img; List<comments> commentsList = new List<EM.Model.comments>();
comments comment1 = new comments();
comment1.userId = ;
comment1.user = "三生石";
comment1.text = "你所有的努力,只是为了更好的衬托别人的成功。";
comment1.createTime = DateTime.Now;
comment1.updateTime = DateTime.Now;
commentsList.Add(comment1); comments comment2 = new comments();
comment2.userId = ;
comment2.user = "影魔";
comment2.text = "明明可以靠脸吃饭,他却偏要靠才华,最后还是饿死了。";
comment2.createTime = DateTime.Now;
comment2.updateTime = DateTime.Now;
commentsList.Add(comment2); news.comments = commentsList; news.createTime = DateTime.Now;
news.updateTime = DateTime.Now; 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" : ,
"tags" : [
"国际",
"编程",
"dota"
],
"image" : {
"url" : "http://www.cnblogs.com/cnki/",
"caption" : "好图",
"type" : "回忆录",
"size" : "3M",
"data" : "dota三人组"
},
"comments" : [
{
"userId" : ,
"user" : "三生石",
"text" : "你所有的努力,只是为了更好的衬托别人的成功。",
"createTime" : ISODate("2016-12-05T13:40:46.142Z"),
"updateTime" : ISODate("2016-12-05T13:40:46.142Z")
},
{
"userId" : ,
"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")
}

增加一条评论

             comments commentsInfo = new comments();
commentsInfo.userId = ;
commentsInfo.user = "mf";
commentsInfo.text = "世界太美好";
commentsInfo.createTime = DateTime.Now;
commentsInfo.updateTime = DateTime.Now;
var update = Builders<News>.Update.Push(m => m.comments, commentsInfo);
bool bUpdateOne = MongoDBServiceFactory.CreateMongoDBService(conStrNo, databaseName).UpdateOne<News>("News", m => m._id == "95565797-8572-4281-b6e6-6820d0c8dc37", update);

结果

 {
"_id" : "95565797-8572-4281-b6e6-6820d0c8dc37",
"title" : "大新闻",
"url" : "http://www.cnblogs.com/cnki/",
"author" : "沐风",
"vote_count" : ,
"tags" : [
"国际",
"编程",
"dota"
],
"image" : {
"url" : "http://www.cnblogs.com/cnki/",
"caption" : "好图",
"type" : "回忆录",
"size" : "3M",
"data" : "dota三人组"
},
"comments" : [
{
"userId" : ,
"user" : "三生石",
"text" : "你所有的努力,只是为了更好的衬托别人的成功。",
"createTime" : ISODate("2016-12-05T13:40:46.142Z"),
"updateTime" : ISODate("2016-12-05T13:40:46.142Z")
},
{
"userId" : ,
"user" : "影魔",
"text" : "明明可以靠脸吃饭,他却偏要靠才华,最后还是饿死了。",
"createTime" : ISODate("2016-12-05T13:40:46.142Z"),
"updateTime" : ISODate("2016-12-05T13:40:46.142Z")
},
{
"userId" : ,
"user" : "mf",
"text" : "世界太美好",
"createTime" : ISODate("2016-12-05T13:44:16.831Z"),
"updateTime" : ISODate("2016-12-05T13:44:16.832Z")
}
],
"createTime" : ISODate("2016-12-05T13:40:46.142Z"),
"updateTime" : ISODate("2016-12-05T13:40:46.142Z")
}

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字段

             //找到子文档
var a = Builders<News>.Filter.Eq(x => x._id, "95565797-8572-4281-b6e6-6820d0c8dc37");
var b = Builders<News>.Filter.ElemMatch(x => x.comments, y => y.userId == );
var filter = Builders<News>.Filter.And(new FilterDefinition<News>[] { a, b });
//更新子文档的字段
var field = new StringFieldDefinition<News, string>("comments.$.comments");
var update = Builders<News>.Update.Set(field, "修改后的我");
//更新库
UpdateResult updateResult = MongoDBServiceFactory.CreateMongoDBService(conStrNo, databaseName).DocumentUpdate("News", filter, update);
bool bupdateResult= updateResult != null && updateResult.ModifiedCount > && 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;