首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET 云原生架构师训练营(模块二 基础巩固 MongoDB 问答系统)--学习笔记

.NET 云原生架构师训练营(模块二 基础巩固 MongoDB 问答系统)--学习笔记

作者头像
郑子铭
发布2021-01-13 16:01:05
3540
发布2021-01-13 16:01:05
举报

2.5.6 MongoDB -- 问答系统

  • MongoDB 数据库设计
  • API 实现概述

MongoDB 数据库设计

设计优化

  • 内嵌(mongo)还是引用(mysql)
  • 数据一致性

范式:将数据分散到不同的集合;反范式:使用内嵌文档

在范式化的情况下需要在进行多次查询再拼装数据,或者使用 lookup,即跨表查询;反范式化的情况下可以直接查出相关数据

更适合内嵌

更适合引用

子文档较小

子文档较大

数据不会定期改变

数据经常改变

最终数据一致即可

中间阶段数据必须一致

文档数据小幅增加

文档数据大幅增加

数据通过需要执行二次查询才能获得

数据通常不包含在结果中

快速读取

快速写入

需求

  • 查询所有问题(根据标签查询,按发布时间,浏览数量、投票数量、降序排序)
  • 创建问题,回答问题
  • 对问题投票,对答案投票
  • 对问题添加评论,对答案添加评论
  • 对问题进行修改,对答案进行修改
  • 我投过票的问题,我投过票的答案
  • 我浏览过的问题
  • 我回答的问题列表

API 实现概述

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
  • 连接字符串
安装 nuget 包
dotnet package install MongoDB.Driver
服务注入 IMongoClient

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

课程链接

.NET云原生架构师训练营讲什么,怎么讲,讲多久

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DotNet NB 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.5.6 MongoDB -- 问答系统
    • MongoDB 数据库设计
      • API 实现概述
        • 创建文档类
        • 集成 mongo db driven
      • GitHub源码链接:
      • 课程链接
      相关产品与服务
      云数据库 MongoDB
      腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档