1

I'm trying to make a Projection right after a Group clause in my C# application but I'm getting "doesn't contain definition for 'Project' " error.

Mongo DB - Code that has worked:

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# - This is the code without project:

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# - With Project (and error)

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'
            }
        ]
    }
]

I expect to get the following object as result of the projection:

{
    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'
                }
            ]
        }
    ]
}
Glenn Mateus
  • 319
  • 1
  • 2
  • 17

2 Answers2

1

I think you have your generic parameters switched up. Project's first generic parameter is the input type, and its second generic parameter is the output type. However, your lambda is returning a DataBaseObject where it should be returning a ProjectObject.

 .Project<DataBaseObject, ProjectObject>(model => new DataBaseOject
              {
                  externalId = model.Id,
                  PV = new List<ProjectObject>()
              })
Victor Wilson
  • 1,720
  • 1
  • 11
  • 22
0

SOLVED:

I solved the problem just casting the List to a List of 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();
Glenn Mateus
  • 319
  • 1
  • 2
  • 17