3

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?

2 Answers 2

1
  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' }​​​​​"))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your service, General. Is there a form where I could remove the strings from the query? Such that the Match's Parse is m=>bar.barCode==barcode. I ask because I've been in similar situation where the fully fluent query compiles, and exceptions at runtime.
1

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/

1 Comment

And how would that be implemented using the C# driver and account for a dynamic filter? That's where I'm stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.