using MongoDB.Bson; using MongoDB.Driver; using System; using System.Linq; namespace TestCSharp1356 { public class C { public int Id; public int[] a; } public static class Program { public static void Main(string[] args) { var client = new MongoClient("mongodb://localhost"); var database = client.GetDatabase("test"); var collection = database.GetCollection("test"); database.DropCollection("test"); var documents = new C[] { new C { Id = 1, a = new int[0] }, new C { Id = 2, a = new [] { 1 } }, new C { Id = 3, a = new [] { 2 } }, new C { Id = 4, a = new [] { 3 } }, new C { Id = 5, a = new [] { 4 } }, new C { Id = 6, a = new [] { 1, 2 } }, new C { Id = 7, a = new [] { 1, 3 } }, new C { Id = 8, a = new [] { 1, 4 } }, new C { Id = 9, a = new [] { 2, 3 } }, new C { Id = 10, a = new [] { 3, 4 } }, new C { Id = 11, a = new [] { 1, 2, 3 } }, new C { Id = 12, a = new [] { 1, 2, 4 } }, new C { Id = 13, a = new [] { 1, 3, 4 } }, new C { Id = 14, a = new [] { 1, 2, 3, 4 } } }; collection.InsertMany(documents); var x = new[] { 1, 2 }; Console.WriteLine("documents.Where(c => c.a.All(a => x.Contains(a)))"); foreach (var match in documents.Where(c => c.a.All(a => x.Contains(a)))) { Console.WriteLine(match.ToJson()); } Console.WriteLine(); Console.WriteLine("documents.Where(c => !c.a.Any(a => !x.Contains(a)))"); foreach (var match in documents.Where(c => !c.a.Any(a => !x.Contains(a)))) { Console.WriteLine(match.ToJson()); } Console.WriteLine(); Console.WriteLine("collection.Find(c => c.a.All(a => x.Contains(a)))"); try { foreach (var match in collection.Find(c => c.a.All(a => x.Contains(a))).ToEnumerable()) { Console.WriteLine(match.ToJson()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(); Console.WriteLine("collection.Find(c => !c.a.Any(a => !x.Contains(a)))"); foreach (var match in collection.Find(c => !c.a.Any(a => !x.Contains(a))).ToEnumerable()) { Console.WriteLine(match.ToJson()); } Console.WriteLine(); var filter = "{ a : { $not : { $elemMatch : { $nin : [ 1, 2 ] } } } }"; Console.WriteLine(filter); foreach (var match in collection.Find(filter).ToEnumerable()) { Console.WriteLine(match.ToJson()); } Console.WriteLine(); Console.ReadLine(); } } }