Reputation: 33
What I want to achieve is to provide List<JobList>
containing client Id and InternalIds of jobs to the Finish()
method.
Then I want to iterate through clients and update all jobs matches JobInternalIds and set current datetime to FinishedAt field.
The problem is that I totally don't know how to update nested objects.
I've tried something like below but without success.
public class Client
{
[BsonId]
public ObjectId Id { get; set;}
public string Name { get; set;}
public List<Job> Jobs { get; set;}
}
public class Job
{
public string InternalId { get; set;}
public string Name { get; set;}
public DateTime? FinishedAt { get; set;}
}
public class JobList
{
public string ClientId { get; set; }
List<string> JobInternalIds { get; set; }
}
public async Task Finish(List<JobList> joblist)
{
var updateDefinition = new UpdateDefinitionBuilder<Client>()
.Set(x => x.Job[0].FinishedAt, DateTime.UtcNow); // don't know how to set datetime to finishedAt for each elements of collection that matches InternalIds
foreach(var item in joblist)
{
await _db.Collection.UpdateManyAsync(item.ClientId, updateDefinition);
}
}
----------EDITED----------
public async Task Finish(List<JobList> joblist)
{
var updateDefinition = Builders<Client>.Update
.Set(x => x.Jobs[-1].FinishedAt, DateTime.UtcNow);
foreach (var item in joblist)
{
var internalIds = item.JobInternalIds.Select(x => x.InternalId).ToList();
var idFilter = Builders<Client>.Filter.Eq(x => x.Id, item.ClientId);
var internalIdsFilter = Builders<Client>.Filter.ElemMatch(x => x.Jobs, y => internalIds.Contains(y.InternalId));
var combinedFilters = Builders<Client>.Filter.And(idFilter, internalIdsFilter);
await _db.Collection.UpdateManyAsync(combinedFilters, updateDefinition);
}
}
Upvotes: 2
Views: 3664
Reputation: 14436
You can use the filtered positional operator to update all elements that match a condition in an array:
var internalIds = new[] { "1", "2" };
var idFilter = Builders<Client>.Filter.Eq(x => x.Id, objectId);
var updateDefinition = Builders<Client>.Update
.Set("Jobs.$[job].FinishedAt", DateTime.UtcNow);
await collection.UpdateManyAsync(idFilter, updateDefinition,
new UpdateOptions
{
ArrayFilters = new []
{
new BsonDocumentArrayFilterDefinition<BsonDocument>(new BsonDocument("job.groupName", new BsonDocument("$in", new BsonArray(internalIds)))),
}
});
Check out this blog post for some examples - https://kevsoft.net/2020/03/23/updating-arrays-in-mongodb-with-csharp.html
Upvotes: 2