天天看點

.NET 雲原生架構師訓練營(子產品二 基礎鞏固 MongoDB 問答系統)--學習筆記2.5.6 MongoDB -- 問答系統

2.5.6 MongoDB -- 問答系統

  • MongoDB 資料庫設計
  • API 實作概述

設計優化

  • 内嵌(mongo)還是引用(mysql)
  • 資料一緻性

範式:将資料分散到不同的集合;反範式:使用内嵌文檔

在範式化的情況下需要在進行多次查詢再拼裝資料,或者使用 lookup,即跨表查詢;反範式化的情況下可以直接查出相關資料

更适合内嵌 更适合引用
子文檔較小 子文檔較大
資料不會定期改變 資料經常改變
最終資料一緻即可 中間階段資料必須一緻
文檔資料小幅增加 文檔資料大幅增加
資料通過需要執行二次查詢才能獲得 資料通常不包含在結果中
快速讀取 快速寫入

需求

  • 查詢所有問題(根據标簽查詢,按釋出時間,浏覽數量、投票數量、降序排序)
  • 建立問題,回答問題
  • 對問題投票,對答案投票
  • 對問題添加評論,對答案添加評論
  • 對問題進行修改,對答案進行修改
  • 我投過票的問題,我投過票的答案
  • 我浏覽過的問題
  • 我回答的問題清單

postman 文檔:

https://documenter.getpostman.com/view/4874930/TVYM3F2M#4e7e4e11-c424-41ce-a463-3d1995a78ff8
api name
GET /api/question 查詢問題清單
GET /api/question/{id} 查詢單個問題
GET /api/question/{id}/answers 查詢單個問題帶答案
POST /api/question 建立問題
PATCH /api/question/{id} 修改問題
POST /api/question/{id}/answer 回答問題/添加答案
POST /api/question/{id}/up 向上投票問題
POST /api/question/{id}/down 向下投票問題
POST /api/question/{id}/comment 添加問題評論
GET /api/answer 查詢答案
POST /api/answer/{id}/up 向上投票答案
POST /api/answer/{id}/down 向下投票答案
PATCH /api/answer/{id} 修改答案
POST /api/answer/{id}/comment 添加答案評論

建立文檔類

  • question
  • answer
  • vote
  • comment
  • view
namespace LighterApi.Data.Question
{
    public class Question : Entity
    {
        public String ProjectId { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public List<string> Tags { get; set; } = new List<string>();

        public int ViewCount { get; set; }

        public int VoteCount { get; set; }

        public List<string> VoteUps { get; set; } = new List<string>();

        public List<string> VoteDowns { get; set; } = new List<string>();

        public List<string> Answers { get; set; } = new List<string>();

        public List<Comment> Comments { get; set; } = new List<Comment>();
    }
}           
namespace LighterApi.Data.Question
{
    public class Answer : Entity
    {
        public string QuestionId { get; set; }

        public string Content { get; set; }

        public int VoteCount { get; set; }

        public List<string> VoteUps { get; set; } = new List<string>();

        public List<string> VoteDowns { get; set; } = new List<string>();

        public List<Comment> Comments { get; set; } = new List<Comment>();
    }
}           
namespace LighterApi.Data.Question
{
    public class Vote : Entity
    {
        public string SourceType { get; set; }

        public string SourceId { get; set; }

        public EnumVoteDirection Direction { get; set; }
    }
}           
namespace LighterApi.Data.Question
{
    public class Comment
    {
        public string Content { get; set; }

        public DateTime CreatedAt { get; set; }

        public string CreatedBy { get; set; }
    }
}           
namespace LighterApi.Data.Question
{
    public class View : Entity
    {
        public string QuestionId { get; set; }
    }
}           
namespace LighterApi.Share
{
    public class ConstVoteSourceType
    {
        public const string Question = "question";

        public const string Answer = "answer";
    }
}           
namespace LighterApi.Share
{
    public enum EnumVoteDirection
    {
        Up = 1,
        Down = 0,
    }
}           

內建 mongo db driven

  • 安裝 nuget 包
  • 服務注入 IMongoClient
  • 連接配接字元串

dotnet package install MongoDB.Driver           

Startup

services.AddSingleton<IMongoClient>(sp =>
{
    return new MongoClient(Configuration.GetConnectionString("LighterMongoServer"));
});           

appsettings.json

"LighterMongoServer": "mongodb://127.0.0.1"           

連接配接到單個執行個體,預設127.0.0.1:27017
var client = new MongoClient();

指定一個連接配接字元串
var client = new MongoClient("mongodb://localhost:27017");

指寫帶密碼的連接配接字元串
var client = new MongoClient("mongodb://admin:password@localhost:27017");

連接配接到一個副本集,用戶端服務發現 
var client = new MongoClient("mongodb://localhost:27017,localhost:27018,localhost:27019"           

GitHub源碼連結:

https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp/LighterApi