如何使用Mongodb C#驱动程序连接多个集合

5
我需要在聚合操作中使用多个$lookup,涉及到3个集合的连接。我在C#驱动程序中尝试过$lookup用户集合,但无法执行第二个涉及设置集合的$lookup
有人能帮忙吗?
db.Transactions.aggregate([
    {
        $lookup:
        {
            from: "Account",
            localField: "AccountId",
            foreignField: "_id",
            as: "Account"
        }
    },
       {
           $lookup:
        {
            from: "User",
            localField: "UserId",
            foreignField: "_id",
            as: "User"
        }
       }
    ])
    .match({
    })
    .project({})

以下是C#代码:

 var account = _dbClient.GetDatabase(_dbName).GetCollection<Account>("Accounts");
var user = _dbClient.GetDatabase(_dbName).GetCollection<User>("Users");
var transaction = _dbClient.GetDatabase(_dbName).GetCollection<Transaction>("Transactions");

var result = (from t in transaction.AsQueryable()
              join a in account.AsQueryable() on t.AccountId equals a.Id
              join u in user.AsQueryable() on t.UserId equals u.Id into userList
              from acc in userList.DefaultIfEmpty()
              where acc.CompanyName.ToLower().Contains(companyName) && c.CreatedDate >= fromDate && c.CreatedDate <= toDate
              select new TransactionHistory
              {
                   Id = t.Id,
                   CompanyName = acc.CompanyName,
                   UserId = u.UserId
                   FirstName = u.FirstName
              }).ToList();

我使用 Linq 时遇到了错误 $project 或 $group 不支持 {document}。

1
我需要对账户集合进行一些筛选,例如使用CompanyName.Contains()。我尝试使用Linq,但它会抛出消息说不支持Contains() - codecodeNinja
2
.Contains(xyz) 肯定是支持的...不确定 Containts()... - jazb
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.contains?view=netframework-4.8 - jazb
1
你说的无法执行第二个 $lookup 是什么意思?能否贴出代码片段以及(如果有的话)错误信息? - Wan B.
@WanBachtiar,它没有任何错误,如果我使用第二个$lookup,它会显示语法错误。 - codecodeNinja
显示剩余2条评论
1个回答

14

我需要使用多个$lookup在聚合中连接3个集合。

给定以下类:

public class Transactions
{
    public ObjectId Id { get; set; }
    public int UserId { get; set; }
    public int AccountId { get; set; }
    public int SettingId { get; set; }
}
public class Account
{
    public int Id {get; set;}
    public int Name {get; set;}
}
public class User
{
    public int Id {get; set;}
    public int Name {get; set;}
}
public class Setting
{
    public int Id {get; set;}
    public int Name {get; set;}
}

您可以使用MongoDB .NET/C# driver(当前版本为v2.9)执行以下多个$lookup阶段:

var collection = database.GetCollection<Transactions>("transactions");

var docs = collection.Aggregate()
                     .Lookup("account", "AccountId", "_id", "asAccounts")
                     .Lookup("user", "UserId", "_id", "asUsers")
                     .Lookup("setting", "SettingId", "_id", "asSettings")
                     .As<BsonDocument>()
                     .ToList();

foreach (var doc in docs) {
    Console.WriteLine(doc.ToJson());
}

你可以在之前/之间/之后添加匹配,如果您想筛选特定的值。请记住,在每个查找阶段之后,文档会被更改。
值得一提的是,如果您需要将多个集合作为常规操作的一部分进行连接,则应重新考虑数据库数据模型。有关更多信息,请参见模式设计:摘要

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接