C#MongoDB.Project未返回字段id的驱动程序聚合
我的问题是查询以下格式的文档:C#MongoDB.Project未返回字段id的驱动程序聚合,c#,.net,mongodb,.net-core,C#,.net,Mongodb,.net Core,我的问题是查询以下格式的文档: { "_id" : NumberLong(21), "_t" : "Detail", "Name": "Bank Z", "DeletionDate" : null, "CreationDate" : ISODate("2017-11-16T13:29:10.160Z"
{
"_id" : NumberLong(21),
"_t" : "Detail",
"Name": "Bank Z",
"DeletionDate" : null,
"CreationDate" : ISODate("2017-11-16T13:29:10.160Z"),
"UpdateDate" : ISODate("2017-11-16T13:29:10.160Z"),
"Accounts" : [
{
"Bank" : "123",
"BankAccount" : "123456"
}
]
}
我有这样的班级结构
Detail.cs
[BsonDiscriminator("Detail")]
public class Detail
{
public virtual long? Id { get; set; }
public string Name { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime? DeletionDate { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime? CreationDate { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime? UpdateDate { get; set; }
private IEnumerable<Account> _accounts;
public IEnumerable<Account> Accounts {
get
{
if (_accounts == null)
return new List<Account>();
return _accounts;
}
set { _accounts = value; }
}
}
public class Account
{
public string Bank { get; set; }
public string BankAccount { get; set; }
}
查询
...
Collection.Aggregate().Match(filter).Project(det =>
new Details
{
Id = det.Id,
Name = det.Name,
UpdateDate = det.UpdateDate,
Accounts = det.Accounts.Where(x => x.Bank.Equals(Bank))
}).FirstOrDefault();
通过这个查询,我可以将几乎所有预填充的数据带到对象实例中,但是_id没有填充,UpdateDate也没有填充。
查看MongoDB.Driver项目的github,我看到谁在Render方法中删除了_id字段。我想知道是否有办法扭转这一局面,谁知道如何知道目的
在mongodb中,查询如下:
db.getCollection('Detail').aggregate([
{ $match: { _id: 21 } },
{
$project: {
UpdateDate: 1,
Name: 1,
SpecificAccounts: { $filter: { input: "$SpecificAccounts", as: "x", cond: { $eq: [ "$$x.Bank", "123" ] } } }
}
}
])
通过将整个查询更改为LINQ,我成功地解决了这个问题,我相信驱动程序在内部使用了另一种渲染方法
var q = from det in Collection.AsQueryable()
where det.Id == 21
select new Details
{
Id = det.Id,
Name = det.Name,
SpecificAccounts = det.SpecificAccounts.Where(x => x.Bank.Equals(getAccountDTO.Bank))
};
var resp = q.ToList().FirstOrDefault();
通过将整个查询更改为LINQ,我成功地解决了这个问题,我相信驱动程序在内部使用了另一种渲染方法
var q = from det in Collection.AsQueryable()
where det.Id == 21
select new Details
{
Id = det.Id,
Name = det.Name,
SpecificAccounts = det.SpecificAccounts.Where(x => x.Bank.Equals(getAccountDTO.Bank))
};
var resp = q.ToList().FirstOrDefault();