0

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.

Xavier W.
  • 1,270
  • 3
  • 21
  • 47
  • Try y.AuthorizationRequestComponents[-1].Comments – dnickless Jan 17 '18 at 09:46
  • @dnickless it works with the `-1`. Can you please answer the question with a link to documentation, because I don't understand where the `-1` comes from. Thank you. – Xavier W. Jan 17 '18 at 09:56
  • I just found the reference again that once tought me this behaviour - see above. So I cannot really take the credits for this one. ;) It's certainly not well documented (you could read through the driver's code, though, if you wanted)... – dnickless Jan 17 '18 at 10:12

0 Answers0