Filter value from BsonDocument (dynamic field) in C#

BsonDocument filtration does not work as expected. I need to filter BsonDocument (I used the BsonDocument field to store JSON object). I need to filter using that json property.

As a example

public class Book
{
        [BsonId]
        public string Id { get; set; }

        public string Author { get; set; }

        [BsonExtraElements]
        public BsonDocument Metadata { get; set; }
}

below json save in Metadata field (BsonDocument)

{
   "name":"John 2",
   "age":30,
   "car":null
}

now I need to filter using name or age field. how can I do that in .net core ? any idea or support

Seems like this is working. let me know if anyone know better solution than this
_books.Find(Builders.Filter.Eq(“user.name”, “Test User”)).ToList()

1 Like

It seems you could also try

var filter = Builders.Filter.Eq(“fieldName.nestedFieldName”, “value”)

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.