【问题标题】:How to join multiple collections using Mongodb C# driver如何使用 Mongodb C# 驱动程序加入多个集合
【发布时间】:2019-08-08 05:18:49
【问题描述】:

我需要将 3 个集合与多个 $lookup 聚合在一起 我在 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 or $group does not support {document}.

【问题讨论】:

  • 为什么不使用LINQ
  • 我需要做一些过滤器,例如 CompanyName.Contains() 来收集帐户。我在Linq 中尝试过,但它向我抛出消息说Containts() 不受支持
  • .Contains(xyz) 肯定受支持...不确定Containts()...
  • 无法执行第二个$lookup是什么意思?您能否发布代码 sn-p,以及您遇到的错误(如果有)?

标签: c# mongodb mongodb-csharp-2.0


【解决方案1】:

我需要使用多个 $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());
}

如果您想过滤特定值,可以在中间/之前/之后添加Match。请记住,在每个 Lookup 阶段之后更改后的文档。

值得一提的是,如果您需要加入多个集合作为常见操作的一部分,您应该重新考虑database data model。请参阅Schema Design: Summary 了解更多信息。

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多