Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 482
Rick DeBay Avatar asked Jul 09 '26 21:07

Rick DeBay


1 Answers

  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' }​​​​​"))
like image 106
BSSchwarzkopf Avatar answered Jul 14 '26 17:07

BSSchwarzkopf