亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

.NET Core利用BsonDocumentProjectionDefinition和Lookup進行 join 關聯查詢(推薦)

瀏覽:168日期:2022-06-09 10:56:05
目錄
  • 前序
  • 1. 準備實體模型
  • 2 .前置連接Mongo代碼
  • 3. 構建BsonDocumentProjectionDefinition
  • 4.利用 Lookup 進行關聯

前序

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

1. 準備實體模型

/// <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>
    /// 構建用戶Dto(不在Mongo創建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. 構建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 進行關聯

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

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

標簽: ASP.NET
主站蜘蛛池模板: 久久爱综合网 | 公么吃奶满足了我苏媚 | 精品视频手机在线观看免费 | 黄色亚洲| 91视频黑丝| 国产大片91精品免费观看不卡 | 久草网免费 | 在线观看日韩欧美 | 麻豆传媒小视频 | 免费日韩视频 | 亚洲欧美日韩综合二区三区 | 免费特黄级夫费生活片 | 中国一级片免费看 | 亚洲国产一区二区三区综合片 | 国产免费福利视频 | 99玖玖| 久久国产精品久久 | 亚洲成人网在线观看 | 午夜剧场毛片 | 欧美另类videosbestsex久久 | 中文字幕 亚洲 一区二区三区 | 黄色片视频在线 | yy6080久久国产伦理 | 免费一级黄色录像影片 | 久久成人免费大片 | 成人短视频在线观看 | 五夜婷婷| 免费一级特黄 欧美大片 | 日本xxxⅹ色视频在线观看网站 | 站长推荐国产午夜免费视频 | 美女拍拍拍无遮挡 | 免费视频精品一区二区 | 亚洲 欧美 精品 | 亚洲精品网站在线观看不卡无广告 | 中日韩免费视频 | 国产高清区| 久久久久欧美精品观看 | 国产一级视频免费 | 99re最新网址 | 欧美国产人妖另类色视频 | 女人被男人狂躁免费视频 |