• .net core-利用BsonDocumentProjectionDefinition和Lookup进行 join 关联查询(MongoDB)


    前序

         前段时间由于项目需要用到MongoDB,但是MongoDB不建议Collection join  查询,网上很多例子查询都是基于linq 进行关联查询。但是在stackoverflow找到一个例子,程序员的朋友们请善于利用google搜索。主要介绍一个查询角色的所有用户的例子。MongoDB创建Collection 和准备数据,请自行处理。

    1. 准备实体模型

     

    复制代码
        /// 
        /// 用户实体(Collection)
        /// 
        public class User
        {
            public Guid UserId { get; set; }
    
            public string UserName { get; set; }
    
            public string Password { get; set; }
    
            public bool IsDelete { get; set; }
    
            public DateTime CreateTime { get; set; }
    
            public Guid RoleId { get; set; }
        }
        /// 
        /// 角色实体(Collection)
        /// 
        public class Role
        {
            public Guid RoleId { get; set; }
    
            public string RoleName { get; set; }
    
            public DateTime CreateTime { get; set; }
        }
        /// 
        /// 构建用户Dto(不在Mongo创建Collection)
        /// 
        public class UserDto
        {
            public Guid UserId { get; set; }
    
            public string UserName { get; set; }
    
            public DateTime CreateTime { get; set; }
    
            public Guid RoleId { get; set; }
    
            public string RoleName { get; set; }
        }
    复制代码

     

    2 .前置连接Mongo代码

               var client = new MongoClient("xxx");
               var database = client.GetDatabase("xxx");

    3. 构建BsonDocumentProjectionDefinition

    复制代码
    BsonDocumentProjectionDefinition projectionDefinition = new BsonDocumentProjectionDefinition(
                            new BsonDocument("UserId", "$UserId")
                           .Add("UserName", "$UserName")
                           .Add("CreateTime", "$CreateTime")
                           .Add("RoleId", "$RoleId")
                           .Add("RoleName", new BsonDocument("$arrayElemAt", new BsonArray().Add("$Role.RoleName").Add(0)))
                        );
    复制代码

    4.利用 Lookup 进行关联

    复制代码
                Guid roleId = Guid.Empty;
                List list = database.GetCollection(typeof(User).Name)
                    .Aggregate()
                    //过滤条件
                    .Match(Builders.Filter.Eq("IsDelete", false))
                    .Match(Builders.Filter.Eq("RoleId", roleId))
                    //连接Role
                    .Lookup(typeof(Role).Name, "RoleId", "RoleId", typeof(UserDto).Name)
                    //查询需要显示的列
                    .Project(projectionDefinition)
                    .As().ToList();
    复制代码

    第一次写博客,各位大佬请指教。

     

  • 相关阅读:
    HTML5七夕情人节表白网页制作【幻化3D相册】HTML+CSS+JavaScript
    100 个常见错误「GitHub 热点速览 v.22.35」
    1027. 最长等差数列(leetcode)
    一次说全COLA应用架构
    23-Vue之事件修饰符
    设计模式之代理模式
    Kaldi 入门使用教程
    Vue3+ts+Vite项目使用mockjs来模拟数据
    Vu3笔记_02setup与常用的Composition API(组合式API)
    Spring:怨种的我和这玩意死磕到半夜12点20的这件事?
  • 原文地址:https://www.cnblogs.com/honglinjia/p/16813761.html