<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          .NET 云原生架構師訓練營(模塊二 基礎鞏固 MongoDB 問答系統(tǒng))--學習筆記

          共 3638字,需瀏覽 8分鐘

           ·

          2021-01-06 12:28

          2.5.6 MongoDB -- 問答系統(tǒng)

          • MongoDB 數據庫設計

          • API 實現概述

          MongoDB 數據庫設計

          設計優(yōu)化

          • 內嵌(mongo)還是引用(mysql)

          • 數據一致性

          范式:將數據分散到不同的集合;反范式:使用內嵌文檔

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

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

          需求

          • 查詢所有問題(根據標簽查詢,按發(fā)布時間,瀏覽數量、投票數量、降序排序)

          • 創(chuàng)建問題,回答問題

          • 對問題投票,對答案投票

          • 對問題添加評論,對答案添加評論

          • 對問題進行修改,對答案進行修改

          • 我投過票的問題,我投過票的答案

          • 我瀏覽過的問題

          • 我回答的問題列表

          API 實現概述

          postman 文檔:https://documenter.getpostman.com/view/4874930/TVYM3F2M#4e7e4e11-c424-41ce-a463-3d1995a78ff8

          apiname
          GET /api/question查詢問題列表
          GET /api/question/{id}查詢單個問題
          GET /api/question/{id}/answers查詢單個問題帶答案
          POST /api/question創(chuàng)建問題
          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添加答案評論

          創(chuàng)建文檔類

          • 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();
          }
          }
          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();
          }
          }
          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

          • 連接字符串

          安裝 nuget 包

          dotnet package install MongoDB.Driver

          服務注入 IMongoClient

          Startup

          services.AddSingleton(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");


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

          GitHub源碼鏈接:

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

          課程鏈接

          .NET云原生架構師訓練營講什么,怎么講,講多久


          歡迎各位讀者加入微信群一起學習交流,
          在公眾號后臺回復“加群”即可~~


          瀏覽 27
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美视频在线观看 | 一级爱爱免费视频 | 一级A片电影A片录像 | 久久夜色精品网站 | 狠狠干|