【发布时间】: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