C# MongoDB match any of elements in an array -
i have documents this:
/* 1 */ { "_id" : objectid("573f3944a75c951d4d6aa65e"), "source" : "ign", "country" : "us" } /* 2 */ { "_id" : objectid("573f3d41a75c951d4d6aa65f"), "source" : "vg", "country" : "norway" } /* 3 */ { "_id" : objectid("573f4367a75c951d4d6aa660"), "source" : "nrk", "country" : "norway" } /* 4 */ { "_id" : objectid("573f4571a75c951d4d6aa661"), "source" : "vg", "country" : "norway" } /* 5 */ { "_id" : objectid("573f468da75c951d4d6aa662"), "source" : "ign", "country" : "us" }
and list of sources this:
list = ['vg', 'ign']
i want return documents source equals 'ign' or 'vg' (any of elements in list)
how can official c# mongodb driver?
assuming using mongodb c# driver version 2.2, can use filterdefinitionbuilder class filter out desired results.
using system.collections.generic; using system.threading.tasks; using mongodb.bson; using mongodb.driver; ... class , method declaration ... imongoclient client = new mongoclient ("mongodb://localhost:27017/test"); imongodatabase database = client.getdatabase("test"); imongocollection<bsondocument> collection = database.getcollection<bsondocument> ("collection"); var filter = builders<bsondocument>.filter.anyin ("source", new[]{"vg", "ign"}); var cursor = await collection.findasync (filter); var docs = cursor.tolist();
docs
hold documents source
either vg
or ign.
based on sample data, have 4 documents.
i'll recommend have @ how find or query data c# driver
Comments
Post a Comment