TGardner
TGardner

Reputation: 176

MongoDB C# Driver Find Object which contains an Obj in an Array with a certain value that meets certain criteria

I've got a mongo object that follows this pattern. Actions is an array of objects. I've excluded the irrelevant fields.

{
  "_id" : 141,
  ...
  "Actions" : [{
      ...
      "Modified" : new Date("Thu, 29 Nov 2012 14:41:20 GMT -08:00"),
      ...
    }]
  ...
}

How do I query this so that I can get a list of objects which contain an object in the actions array who's modified property falls between a date range using the C# Mongo Driver.

Upvotes: 1

Views: 1346

Answers (1)

TGardner
TGardner

Reputation: 176

I figured this out myself.

Query.ElemMatch("Actions",
    Query.And(
        Query.GTE("Modified", start),
        Query.LTE("Modified", end)
    )
)

Using a Query.ElemMatch against the Actions field passing in the Query for the Sub Elements.

Worked like a charm.

Upvotes: 1

Related Questions