Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#MongoDB.Project未返回字段id的驱动程序聚合_C#_.net_Mongodb_.net Core - Fatal编程技术网

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();