Closed iappwebdev closed 11 months ago
I found another way doing this. In fact I have all necessary informations in the .UpdateFromContext()
-Method. So in there I can perform a standard MongoDb-Update.
The only thing I could not figure out, how to return an "empty" Update operation, so I do an additional call to update.Set()
. Maybe someone has a hint how to solve this better?
On<ItemQuantityChanged>(
builder =>
{
return builder
.UpdateOne
.Filter(
(ctx, filter) => filter.ElemMatch(
pickupstation => pickupstation.Shelves,
Builders<Shelve>.Filter.Eq(a => a.Reservation!.Id, ctx.Stream.GetId())
)
)
.UpdateFromContext(
async (ctx, update) =>
{
var updateDefinition = Builders<PickupstationDocument>
.Update
.Set("Shelves.$[shelve].Reservation.Articles.$[article].Quantity", ctx.Message.NewQuantity);
var arrayFilterDefinitions = new List<ArrayFilterDefinition>
{
new BsonDocumentArrayFilterDefinition<BsonDocument>(
new BsonDocument("shelve.Reservation.Id", ctx.Stream.GetId())
),
new BsonDocumentArrayFilterDefinition<BsonDocument>(
new BsonDocument("article.Name", ctx.Message.ArticleName)
)
};
MongoDB.Driver.UpdateResult updateResult = await Collection
.UpdateOneAsync(
x => x.Shelves.Any(shelve => shelve.Reservation != null && shelve.Reservation!.Id == ctx.Stream.GetId()),
updateDefinition,
new UpdateOptions
{
ArrayFilters = arrayFilterDefinitions
}
)
.ConfigureAwait(true);
return update.Set(x => x.LastChanged, ctx.Created);
}
);
}
);
Nevertheless the original problem is fixed.
Hey,
I would like to use
ArrayFilters
in aEventHandler
forMondoDB
. I have aPickupstationDocument
with shelves for articles to pick up. My goal is to react on a eventItemQuantityChanged
on aReservation-Stream
.I have to use
ArrayFilters
because I need two positional operators: one for the shelve with the matchingReservationId
and one for the article with the matchingArticeName
.The corresponding event looks like this:
The problem is that I can specify ArrayFilters in the
UpdateBuilder<TBuilder>.Configure()
-Method, but I do not have access to theMessageConsumeContext
, as it is the case for.Filter()
and.Update()
. So I cannot dynamically specify the values forReservationId
andArticeName
.It would be nice to have an additonal method like
.ConfigureFromContext
. Is there any change to get this working? Maybe it is also considerable toThe PickupstationDocument
My attempt with
ArrayFilters
with static valuesThe mentioned methods in
UpdateOneBuilder