色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

.NET Core利用BsonDocumentProjectionDefinition和Lookup進行 join 關(guān)聯(lián)查詢(推薦)

瀏覽:219日期:2022-06-09 10:56:05
目錄
  • 前序
  • 1. 準(zhǔn)備實體模型
  • 2 .前置連接Mongo代碼
  • 3. 構(gòu)建BsonDocumentProjectionDefinition
  • 4.利用 Lookup 進行關(guān)聯(lián)

前序

前段時間由于項目需要用到MongoDB,但是MongoDB不建議Collection join 查詢,網(wǎng)上很多例子查詢都是基于linq 進行關(guān)聯(lián)查詢。但是在stackoverflow找到一個例子,程序員的朋友們請善于利用google搜索。主要介紹一個查詢角色的所有用戶的例子。MongoDB創(chuàng)建Collection 和準(zhǔn)備數(shù)據(jù),請自行處理。

1. 準(zhǔn)備實體模型

/// <summary>
    /// 用戶實體(Collection)
    /// </summary>
    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; }
    }
    /// <summary>
    /// 角色實體(Collection)
    /// </summary>
    public class Role
    {
public Guid RoleId { get; set; }

public string RoleName { get; set; }

public DateTime CreateTime { get; set; }
    }
    /// <summary>
    /// 構(gòu)建用戶Dto(不在Mongo創(chuàng)建Collection)
    /// </summary>
    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. 構(gòu)建BsonDocumentProjectionDefinition

BsonDocumentProjectionDefinition<BsonDocument> projectionDefinition = new BsonDocumentProjectionDefinition<BsonDocument>(
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 進行關(guān)聯(lián)

Guid roleId = Guid.Empty;
    List<UserDto> list = database.GetCollection<BsonDocument>(typeof(User).Name)
.Aggregate()
//過濾條件
.Match(Builders<BsonDocument>.Filter.Eq("IsDelete", false))
.Match(Builders<BsonDocument>.Filter.Eq("RoleId", roleId))
//連接Role
.Lookup(typeof(Role).Name, "RoleId", "RoleId", typeof(UserDto).Name)
//查詢需要顯示的列
.Project(projectionDefinition)
.As<UserDto>().ToList();

到此這篇關(guān)于.NET Core利用BsonDocumentProjectionDefinition和Lookup進行 join 關(guān)聯(lián)查詢的文章就介紹到這了,更多相關(guān).net core  join 關(guān)聯(lián)查詢內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: ASP.NET
主站蜘蛛池模板: 香蕉一区二区三区观 | 午夜精品久视频在线观看 | 国产亚洲欧美成人久久片 | 国产精品爽爽va在线观看无码 | 欧美精品亚洲精品日韩专区 | 亚洲一区中文字幕 | 亚洲国产精品久久精品成人 | 性色xxx| 国产在线91精品天天更新 | 清纯偷拍精品视频在线观看 | 国产精品久久久久久久久久免费 | 日韩欧美不卡在线 | 黄色毛片子 | 久久久久琪琪去精品色村长 | 精品国产欧美一区二区最新 | 不卡午夜视频 | 在线观看日韩www视频免费 | 99久久免费看国产精品 | 免费看真人a一级毛片 | 亚洲综合网在线观看首页 | 亚洲日本va午夜中文字幕一区 | 国产精品久久久久久久久福利 | 中文乱码一二三四有限公司 | 一级毛片成人午夜 | 国产高清一区二区三区视频 | 中国一级毛片免费观看 | 色琪琪一本到影院 | 三级黄色毛片网站 | 男人的天堂亚洲 | 午夜伦y4480影院中文字幕 | 欧美手机看片 | 99精品偷自拍 | 在线观看的黄网 | 国产91亚洲精品 | 日本高清无吗免费播放 | 欧美激情 自拍 | 久草在线视频免费 | 午夜不卡av免费 | 在线一区视频 | 狠狠色狠狠色综合 | 欧美高清免费精品国产自 |