我有一个Shipment对象,其中包含材料对象的嵌套列表(请注意:_id表示shippingId)
"_id" : "1",
"createddate" : "2018-10-18T16:25:59.245Z",
"attributes" : {
"materials" : [
{
"materialcode" : "string",
"status" : "Unmatched"
}
]
},
如果状态=不匹配,我想删除相应货件的材料。我试图使用pull或pullfilter只是不确定如何使用
这是我到目前为止所拥有的((由于无法获取无法将lamda表达式转换为字段表达式而导致的错误,因此无法构建):
Task<bool> DeleteShipmentMaterials(string ShipmentId, string MaterialId)
{
var shipment = await GetShipment(ShipmentId);
foreach (var material in shipment.attributes.Materials)
{
var update = Builders<Shipment>.Update.PullFilter(x => x.attributes.Materials, Builders<Shipment>.Filter.Eq(x => x.Id, shipment.Id));
.MaterialCode == material.MaterialCode), );
await UpdateOneAsync(c => c.Id == shipment.Id, update, true);
}
答案 0 :(得分:0)
此处无需使用conversion from ‘__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}’ to non-scalar type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ requested
循环。您可以只使用$pull一个指定条件-这将删除所有具有指定状态的元素。在Mongo Shell中,您可以运行:
foreach
在C#代码中,您可以使用db.shipments.update(
{ "_id" : "1" },
{ $pull: { "attributes.materials": { "status": "Unmatched" } } }
)
方法来构建过滤器。试试:
PullFilter