【问题标题】:Projection after Group组后投影
【发布时间】:2019-08-16 18:53:35
【问题描述】:

我试图在我的 C# 应用程序中的 Group 子句之后立即进行投影,但我收到“不包含 'Project' 的定义”错误。

Mongo DB - 有效的代码:

var pipeline = 
[
    {
        $match:{
            externalId: ObjectId("5d1bc6a0fba276130421a415")
        }
    },
    {
        $group:{
            _id: '$externalId',
            nestedArray:{
                $push:{
                    name: '$CodProcesso',
                    cars: '$AndamentosProcesso'
                }
            }
        }
    },
    {
        $project:{
            _id: 0,
            IdAgenda: '$_id',
            ProcessosVinculados: 1
        }
    }
];
db.MyCollection.aggregate(pipeline);

C# - 这是没有项目的代码:

var return = db.Set<DataBaseObject>(CollectionName)
               .Aggregate()
               .Match(doc => doc.Id.Equals(id))
               .Group(doc => doc.Id, g => new
               {
                   Id = g.Key,
                   nestedArray = g.Select(x => new
                   {
                       x.Name,
                       x.cars
                   })
               })
               .FirstOrDefault();

C# - 带有项目(和错误)

var return = db.Set<DataBaseObject>(CollectionName)
               .Aggregate()
               .Match(doc => doc.Id.Equals(id))
               .Group(doc => doc.Id, g => new
               {
                   Id = g.Key,
                   nestedArray = g.Select(x => new
                   {
                       x.Name,
                       x.cars
                   })
               })
               .Project<DataBaseObject, ProjectObject>(model => new DataBaseOject
                {
                    externalId = model.Id,
                    PV = new List<ProjectObject>()
                })
                .FirstOrDefault();

DataBaseObject:

[
    //Object 1
    {
        "_id" : ObjectId("5d5696aacb865b4ce4aa0689"),
        "externalId" : ObjectId("5d1bc6a0fba276130421a415"),
        "name" : 35223,
        "cars" : [
            {
                aquisitionDate: IsoDate("2019-08-16"),
                new: true,
                model: 'Mustang'
            }
        ]
    },
    //Object 2
    {
        "_id" : ObjectId("5d5696aacb865b4ce4aa0689"),
        "externalId" : ObjectId("5d1bc6a0fba276130421a415"),
        "name" : 35223,
        "cars" : [
            {
                aquisitionDate: IsoDate("2019-08-16"),
                new: true,
                model: 'Ferrari'
            },
            {
                aquisitionDate: IsoDate("2019-08-16"),
                new: true,
                model: 'Lamborghini'
            }
        ]
    }
]

我希望得到以下对象作为投影的结果:

{
    externalId: ObjectId("5d1bc6a0fba276130421a415"),
    nestedArray:[
        {
            name: 'Adrian',
            cars: [
                {
                    aquisitionDate: IsoDate("2019-08-16"),
                    new: true,
                    model: 'Mustang'
                },
                {
                    aquisitionDate: IsoDate("2019-08-16"),
                    new: true,
                    model: 'Ferrari'
                },
                {
                    aquisitionDate: IsoDate("2019-08-16"),
                    new: true,
                    model: 'Lamborghini'
                }
            ]
        }
    ]
}

【问题讨论】:

    标签: c# mongodb aggregation-framework mongodb-csharp-2.0


    【解决方案1】:

    我认为您的通用参数已切换。 Project 的第一个泛型参数是输入类型,它的第二个泛型参数是输出类型。但是,您的 lambda 正在返回一个 DataBaseObject,它应该返回一个 ProjectObject

     .Project<DataBaseObject, ProjectObject>(model => new DataBaseOject
                  {
                      externalId = model.Id,
                      PV = new List<ProjectObject>()
                  })
    

    【讨论】:

      【解决方案2】:

      已解决:

      我解决了将列表转换为 NestedArrayObject 列表的问题。

      var return= db.Set<DataBaseObject>(CollectionName)
                    .Aggregate()
                    .Match(doc => doc.Id.Equals(id))
                    .Group(doc => doc.Id, g => new
                    {
                        Id = g.Key,
                        nestedArray = g.Select(x => new 
                        {
                            x.name,
                            x.cars
                        })
                    })
                    .Project(model => new ProjectObject
                    {
                        externalId = model.Id,
                        nestedArray = (List<NestedArrayObject>) model.nestedArray.Select(pv => new
                       {
                            pv.name,
                            cars = pv.cars.Where(a=>a.new\)
                        })
                    }).FirstOrDefault();
      

      【讨论】:

      • 我忘了取消删除我的答案。不过,您似乎已经发现了您的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-12
      • 2018-03-15
      相关资源
      最近更新 更多