MongoDB field-array searching (C#, How to?)

核能气质少年 提交于 2019-12-10 16:12:58

问题


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

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

Thank you very much!!!


回答1:


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);

The result of Query.All will all documents thats have nested array SomeArray with values 3 and 4.

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

References to the documentation: $all, $in




回答2:


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

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


来源:https://stackoverflow.com/questions/5457637/mongodb-field-array-searching-c-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!