Search in MongoDB field arrays (C #, How to?)

Please tell me how to do a field search? I have fields of type List<Int64> . For example, the first document has a field array with numbers [1,2,3,4], and the second document has such a field with numbers [4,5,6,7].

I want to find documents where my field consists of 3 and 4 numbers, so this is the first document. I am looking for examples based on the official C # MongoDB driver;)

Thank you very much!!!

+4
source share
2 answers

You should use Query.All() . Code like this:

 var array = new List<int>() {3, 4}; var query = Query.All("SomeArray", new BsonArray(array)); collection.Find(query); 

Result Query.All will have all documents that have a nested array SomeArray with values 3 and 4 .

If you want 3 or 4 use Query.In("SomeArray", new BsonArray(array))

Documentation links: $ all , $ in

+8
source

In recent versions of the C # driver, you can use:

 Query<MyType>.All(_ => _.MyPropertyName, array)); 
+1
source

Source: https://habr.com/ru/post/1345525/


All Articles