Reputation: 692
This might be very simple, however I am having a hard time figuring it out:
I have a List<Tuple<String, String>>
that contains a list of products that I need to search for, and I would like to get all documents of all products that is represented in that list in one request.
Item1
is the SKU of the product, but because there can be duplicate SKU's, i also have Item2
which contains which SupplierId it should look for.
My challenge is to build the Query with the MongoDB C# Driver, to get the data out, anyone that can help?
I am using the new 2.3.0 driver version, which doesn't have much exsiting help on this subject.
Here is the code I have so far:
var collection = _database.GetCollection<StockDoc>("stock");
var result = collection.Find().ToListAsync().Result;
Upvotes: 1
Views: 69
Reputation: 9679
I assume your StockDoc class as:
public class StockDoc
{
public ObjectId Id { get; set; }
public string SKU { get; set; }
public string SupplierId { get; set;}
}
I would write a help method that creates filter for each tuple from the list (it's and filter: SKU = Item1 && SupplierId == Item2
):
public FilterDefinition<StockDoc> BuildFilter(Tuple<String, String> p)
{
return Builders<StockDoc>.Filter.And(
Builders<StockDoc>.Filter.Eq(x=>x.SKU, p.Item1),
Builders<StockDoc>.Filter.Eq(x=>x.SupplierId, p.Item2) );
}
After that you could build an Or
filter to get items for all tuples from the list:
var p = new List<Tuple<String, String>> {
Tuple.Create("a", "1"),
Tuple.Create("b", "1"),
Tuple.Create("d", "2")};
var filter = Builders<StockDoc>.Filter.Or(p.Select(BuildFilter));
After that you could get your data with this filter:
collection.Find(filter).ToList()
Upvotes: 1