I have a mongo collection of AuthorizationRequest
including a list of AuthorizationRequestComponent
as subdocument. It it possible to add a list of Comment
for each AuthorizationRequestComponent
.
In my repository part, I want to insert a list of Comment
in a specific AuthorizationRequestComponent
using the following code :
public void AddAuthorizationRequestComponentComments(ObjectId id, string componentCode, List<Comment> comments)
{
var filter = Builders<AuthorizationRequest>.Filter.And(
Builders<AuthorizationRequest>.Filter.Eq(x => x.TechnicalId, id),
Builders<AuthorizationRequest>.Filter.ElemMatch(c => c.AuthorizationRequestComponents, c => c.Code == componentCode)
);
var update = Builders<AuthorizationRequest>.Update.PushEach(y => y.AuthorizationRequestComponents[0].Comments, comments);
_authorizationRequestCollection.UpdateOne(filter, update);
}
Unfortunatelly, comments are all time added to the first subdocument of my object. It must be related to the AuthorizationRequestComponents[0].Comments
or the ElemMatch
. I found some information on stackoverflow about this, but it's all time using a Builders<BsonDocument>
instead of the real object.