C# MongoDB filter a value from the following structure

Hi, @Matan_Cohen,

Welcome to the MongoDB Community. I understand that you’re having trouble querying a document by the value in an array element. I wrote the following minimal repro and was able to successfully query documents from a collection with a similar structure:

using MongoDB.Bson;
using MongoDB.Driver;

var client = new MongoClient();
var db = client.GetDatabase("test");
var coll = db.GetCollection<BsonDocument>("coll");

var filter = Builders<BsonDocument>.Filter.Eq("Parameters.Value", 11111);
var query = coll.Find(filter);
foreach (var result in query.ToList())
{
    Console.WriteLine(result);
}

Note that your C# filter contains the value "11111" (data type is string) but your schema appears to contain values in the array of type int. In my filter above, I use the numeric value 11111 to query matching documents.

When querying data from MongoDB, you must either use the correct data types in your query or explicitly convert values to a common data type prior to comparison.

If you continue to have problems with this query, please provide a self-contained repro as well as sample data so we can troubleshoot further.

Sincerely,
James

1 Like