组后投影
Projection after Group
我试图在我的 C# 应用程序中的 Group 子句之后立即进行投影,但出现 "doesn't contain definition for '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();
数据库对象:
[
//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'
}
]
}
]
}
我认为您已打开通用参数。 Project
的第一个泛型参数是输入类型,第二个泛型参数是输出类型。但是,您的 lambda 返回一个 DataBaseObject
它应该返回一个 ProjectObject
.
.Project<DataBaseObject, ProjectObject>(model => new DataBaseOject
{
externalId = model.Id,
PV = new List<ProjectObject>()
})
已解决:
我只是将列表转换为 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();
我试图在我的 C# 应用程序中的 Group 子句之后立即进行投影,但出现 "doesn't contain definition for '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();
数据库对象:
[
//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'
}
]
}
]
}
我认为您已打开通用参数。 Project
的第一个泛型参数是输入类型,第二个泛型参数是输出类型。但是,您的 lambda 返回一个 DataBaseObject
它应该返回一个 ProjectObject
.
.Project<DataBaseObject, ProjectObject>(model => new DataBaseOject
{
externalId = model.Id,
PV = new List<ProjectObject>()
})
已解决:
我只是将列表转换为 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();