0
CommentCollection
{
 "_id":"5b63f0f23846b70011330889",
  "CommentType":"task",
  "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
  "Threads":[
  {
     "_id":"69bcef71-3695-4340-bdec-4a6e4c58c490",
     "CommentType":"task",
     "UserId":ObjectId("52ffc4a5d85242602e000000"),         
     "CommentByUserType":"Admin",
     "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
     "Content":"fdffd",

  },
  {
     "_id":"69bcef71-3695-4340-bdec-4a6e4c58c490",
     "CommentType":"task",
     "UserId":ObjectId("52ffc4a5d85242602e000000"),         
     "CommentByUserType":"Admin",
     "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
     "Content":"fdffd",

  }
 ]
}

Here I have to write a Mongodb filter query from asp.net core based on two conditions, first I want to get CommentCollection by EntityReferenceId, then want to find the specific thread by id from the first result.

Any help will be appreciated.

Ajas Aju
  • 725
  • 7
  • 30

1 Answers1

0

I got the answer,I have wrote my method like below

 public async Task<bool> UpdateCommentAsync(Threads thread)
    {
        var builder = Builders<Comments>.Filter;
        var filter = builder.Empty;
        var update = Builders<Comments>.Update.Set("Threads.$[i].Content", thread.Content);
        var arrayFilters = new List<ArrayFilterDefinition> { new JsonArrayFilterDefinition<Threads>("{'i._id': '" + thread.Id + "'}") };
        var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
        var result = await _context.Comments.UpdateManyAsync(filter, update, updateOptions);
        return result.ModifiedCount > 0;
    }
Ajas Aju
  • 725
  • 7
  • 30