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

149
Views
Find MongoDB document and only matching array elements w/ C# driver

I'm trying to return a document, and that document should have it's array filtered such that it only contains one item. I've seen many similar questions, but none of them deal with dynamic queries. There may be several constraints so I have to be able to keep adding to the filter.

{
  "_id" : ObjectId("6058f722e9e41a3d243258dc"),
  "fooName" : "foo1",
  "fooCode" : 1,
  "bar" : [
    {
      "barCode" : "123",
      "barName" : "Rick's Cafe",
      "baz" : [
        {
          "bazId" : "00",
          "bazDescription" : "Ilsa"
        },
        {
          "bazId" : "21",
          "bazDescription" : "Victor"
        }
      ]
    },
    {
      "barCode" : "456",
      "barName" : "Draco Tavern",
      "baz" : [
        {
          "bazId" : "00",
          "bazDescription" : "Rick Shumann"
        }
      ]
    }
  ]
}

This is my attempt, it returns a document who's array contains the barCode, and the array's entire contents are included.

Expression<Func<Foo, bool>> filter = x => x.FooCode == 1;

string barCode = "456"
if (!String.IsNullOrEmpty(barCode))
{
  Expression<Func<Foo, bool>> newPred =
    x => x.Bar.Any(s => s.BarCode == barCode);
  filter = filter.CombineAnd(newPred);
}

var fooQuery =
  _fooCollection
    .Find(filter);

How do I remove non-matching array elements, but only if an array element was specified?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You need to use aggregate in MongoDB. You can split the array elements with unwind, filter with match, select the keys that you want with project and group with common column like id or something.

MongoDB Aggregation docs: https://docs.mongodb.com/manual/aggregation/

over 4 years ago · Santiago Trujillo Report

0

  1. Unwind to convert the single document into a document per nested-array element in the shape of:
{
  "_id" : ObjectId("6058f722e9e41a3d243258dc"),
  "fooName" : "foo1",
  "fooCode" : 1,
  "bar": {
    "barCode" : "123",
    "barName" : "Rick's Cafe",
    ...
  }
}
  1. Match to find the element you want
  2. Group to recombine the nested-array

So the resulting C# might look like:

var fooQuery = _fooCollection.Aggregate()
                .Unwind("bar")
                .Match(BsonDocument.Parse("{ 'bar.barcode': '"+ barCode + "'}"))
                .Group(BsonDocument.Parse("{​​​​​ '_id':'$fooCode' }​​​​​"))
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!