如何使用 Mongodb C# 驱动程序加入多个集合
How to join multiple collections using Mongodb C# driver
我需要将 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}.
。
I need to join 3 collections in aggregation with multiple $lookup
给出以下 类:
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;}
}
你可以执行多个$lookup stage as below using MongoDB .NET/C# driver(目前是v2.9):
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());
}
如果您想过滤特定值,可以在 between/before/after 中添加 Match。请记住,每个 Lookup
阶段后更改后的文件。
值得一提的是,如果您需要加入多个集合作为常用操作的一部分,您应该重新考虑 database data model. Please see Schema Design: Summary 以获取更多信息。
我需要将 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}.
。
I need to join 3 collections in aggregation with multiple $lookup
给出以下 类:
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;}
}
你可以执行多个$lookup stage as below using MongoDB .NET/C# driver(目前是v2.9):
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());
}
如果您想过滤特定值,可以在 between/before/after 中添加 Match。请记住,每个 Lookup
阶段后更改后的文件。
值得一提的是,如果您需要加入多个集合作为常用操作的一部分,您应该重新考虑 database data model. Please see Schema Design: Summary 以获取更多信息。