Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Getting all documents from a property-combo

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;
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!