MongoDB无法确定lambda的序列化信息

时间:2016-07-05 06:10:52

标签: c# mongodb aggregation-framework mongodb-.net-driver

我需要使用Mongo C#驱动程序构建一个复杂的查询。我已经有了intellishell的工作查询。

db.Organizations.aggregate(
// Pipeline
[
    // Stage 1
    {
    $match: { 
        "$and" : [
            {
                "Country.Path" : /^Asia.*/i
            }, 
            {
                "$or" : [
                    {
                        "OrganizationTypes.Path" : /^Investor.*/i
                    }, 
                    {
                        "OrganizationTypes.Path" : /^Companies.*/i
                    }
              ]
          }, 
          {
              "$or" : [
                  {
                      "Sectors.Path" : /.*Consumer Discretionary\\Recreation Facilities & Svcs.*/i
                  }, 
                  {
                      "Sectors.Path" : /.*Consumer Discretionary\\Consumer Services.*/i
                  }
              ]
          }
      ]
  }
},

// Stage 2
{
  $project: { 
      "id" : 1, 
      "Name" : 1, 
      "OrganizationTypes" : 1, 
      "NumberOfSectors" : {
          "$size" : "$Sectors"
      }, 
      "NumberOfOrganisationType" : {
          "$size" : "$OrganizationTypes"
      }, 
      "NumberOfContacts" : {
          "$size" : "$Contacts"
      }
  }
},

// Stage 3
{
  $match: 
    { "NumberOfSectors": { $lt: 16 } }

},

// Stage 4
{
    $unwind: "$OrganizationTypes"
},

// Stage 5
{
  $group: { 
      "_id" : "$OrganizationTypes.Path",  
      count: { $sum: 1 }
  }
}
]
 );

我的c#代码是:

        List<FilterDefinition<Organization>> filters = new List<FilterDefinition<Organization>>();

        if (filter.CountriesArea != null && filter.CountriesArea.Count > 0)
        {
            filters.Add(Builders<Organization>.Filter.In("CountryAreas._id", ReferenceTreeHelper.Flatten(filter.Countries).Select(x => x.Id)));
        }
        if (filter.Countries != null && filter.Countries.Count > 0)
            filters.Add(Builders<Organization>.Filter.In(x => x.Country.Id, ReferenceTreeHelper.Flatten(filter.Countries).Select(x => x.Id)));

        if (filter.Sectors != null && filter.Sectors.Count > 0)
        {
            filters.Add(Builders<Organization>.Filter.In("Sectors._id", ReferenceTreeHelper.Flatten(filter.Sectors).Select(x => x.Id)));
        }

        if (filter.Precision < 4)
        {
            int value = ((filter.Sectors != null) ? filter.Sectors.Count() : 0) + filter.Precision;
            filters.Add(Builders<Organization>.Filter.Lte(x => x.RootSectorsCount, value));
        }

        if (filter.OrganizationTypes != null && filter.OrganizationTypes.Count > 0)
            filters.Add(Builders<Organization>.Filter.In("OrganizationTypes._id", ReferenceTreeHelper.Flatten(filter.OrganizationTypes).Select(x => x.Id)));

     ProjectionDefinition<Organization> proj = "{ Id : 1, OrganisationName : 1, OrganizationTypes : 1, BusinessSectorsCount : {$size : $Sectors}, OrganisationTypesCount : {$size : $OrganizationTypes}, ContactsCount : {$size : $Contacts} }";

     result = await _Repository.Collection.Aggregate()
                .Match(Builders<Organization>.Filter.And(filters))
                .Project(proj)
                .Unwind<SalesCounterDetailsItem>("OrganizationTypes") 
                .Group(x => x, g => new SalesCounterListItem
                {
                    Id = g.Key.Id,
                    Path = g.Key.OrganizationTypes.Path,
                    Count = g.Key.OrganisationTypesCount
                })
                .ToListAsync();

我对投影有疑问。它会抛出消息Unable to determine the serialization information for x => x.Sectors.Count的异常,这就是stackTrace

   at MongoDB.Driver.ExpressionFieldDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.OperatorFilterDefinition`2.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.AndFilterDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.AggregateFluent`2.<>c__DisplayClass13_0.<Match>b__0(IBsonSerializer`1 s, IBsonSerializerRegistry sr)
   at MongoDB.Driver.DelegatedPipelineStageDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.PipelineStageDefinition`2.MongoDB.Driver.IPipelineStageDefinition.Render(IBsonSerializer inputSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.PipelineStagePipelineDefinition`2.Render(IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry)
  at MongoDB.Driver.MongoCollectionImpl`1.<AggregateAsync>d__20`1.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
  at MongoDB.Driver.IAsyncCursorSourceExtensions.<ToListAsync>d__16`1.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at FinQuest.TeamPortal.Controllers.SalesCounterController.<GetOrganisationTypesCounter>d__3.MoveNext() in D:\Projects\FinQuestPlatform\01-Presentation\FinQuest.TeamPortal\Controllers\SalesCounterController.cs:line 83

对于帖子的篇幅感到抱歉,但我认为你有所有的信息可以帮助我(我希望!) 提前致谢

编辑:添加组织数据样本

{ 
"_id" : BinData(4, "e2Ay8eLeT0yUgZF7x8eErw=="), 
"Name" : "Toto",  
"ZipCode" : "1001", 
"City" : "Islamabad", 
"Country" : {
    "_id" : "C4826BE4-DCD0-4DAE-A159-A580A81BCFE9", 
    "DisplayName" : "Pakistan", 
    "Parents" : [
        {
            "_id" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4", 
            "DisplayName" : "Asia Pacific Rim"
        }, 
        {
            "_id" : "07A45302-9444-459B-B108-7C1EF16B623A", 
            "DisplayName" : "Southern Asia"
        }
    ], 
    "Path" : "Asia Pacific Rim\\Southern Asia\\Pakistan", 
    "PathId" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4\\07A45302-9444-459B-B108-7C1EF16B623A\\C4826BE4-DCD0-4DAE-A159-A580A81BCFE9"
}, 
"OrganizationTypes" : [
    {
        "_id" : "77E35293-992E-4587-94DF-9B0CDC06DDCE", 
        "DisplayName" : "Transactions Services (TS) department of an accounting firm", 
        "Parents" : [
            {
                "_id" : "7EE2315D-9B4D-447F-97B5-EA21AC71AC55", 
                "DisplayName" : "Advisors"
            }, 
            {
                "_id" : "C948A2E4-852A-4B14-A1ED-65C2B2CB6EC4", 
                "DisplayName" : "Financial Services providers (FS providers)"
            }
        ], 
        "Path" : "Advisors\\Financial Services providers (FS providers)\\Transactions Services (TS) department of an accounting firm", 
        "PathId" : "7EE2315D-9B4D-447F-97B5-EA21AC71AC55\\C948A2E4-852A-4B14-A1ED-65C2B2CB6EC4\\77E35293-992E-4587-94DF-9B0CDC06DDCE"
    }
], 
"Sectors" : [
    {
        "_id" : "AAFF7B87-76A1-4B5B-95AD-80C65B03AA21", 
        "DisplayName" : "Telecom", 
        "Parents" : [
            {
                "_id" : "C093170A-7898-430F-8902-FFBD0CD69194", 
                "DisplayName" : "Communications"
            }
        ], 
        "Path" : "Communications\\Telecom", 
        "PathId" : "C093170A-7898-430F-8902-FFBD0CD69194\\AAFF7B87-76A1-4B5B-95AD-80C65B03AA21"
    }, 
    {
        "_id" : "C57D5E33-6E65-4BE0-801A-F4273F0955F3", 
        "DisplayName" : "Apparel & Textile Products", 
        "Parents" : [
            {
                "_id" : "50E99257-723C-4780-987A-E71124001EFC", 
                "DisplayName" : "Consumer Discretionary"
            }
        ], 
        "Path" : "Consumer Discretionary\\Apparel & Textile Products", 
        "PathId" : "50E99257-723C-4780-987A-E71124001EFC\\C57D5E33-6E65-4BE0-801A-F4273F0955F3"
    }, 
    {
        "_id" : "7C05D920-AA39-427F-840E-69C03ADBE55C", 
        "DisplayName" : "Consumer Products", 
        "Parents" : [
            {
                "_id" : "22A6DD29-66B2-4D22-A2BD-A441CF674926", 
                "DisplayName" : "Consumer Staples"
            }
        ], 
        "Path" : "Consumer Staples\\Consumer Products", 
        "PathId" : "22A6DD29-66B2-4D22-A2BD-A441CF674926\\7C05D920-AA39-427F-840E-69C03ADBE55C"
    }
], 
"CountryAreas" : [
    {
        "_id" : "C4826BE4-DCD0-4DAE-A159-A580A81BCFE9", 
        "DisplayName" : "Pakistan", 
        "Parents" : [
            {
                "_id" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4", 
                "DisplayName" : "Asia Pacific Rim"
            }, 
            {
                "_id" : "07A45302-9444-459B-B108-7C1EF16B623A", 
                "DisplayName" : "Southern Asia"
            }
        ], 
        "Path" : "Asia Pacific Rim\\Southern Asia\\Pakistan", 
        "PathId" : "5834EAA5-9B81-497D-A2BB-97BC3CBC79B4\\07A45302-9444-459B-B108-7C1EF16B623A\\C4826BE4-DCD0-4DAE-A159-A580A81BCFE9"
    }
]

}

0 个答案:

没有答案