3

I have the following query:

db.MyData.find( { "SubItems.EndDate" : { $lte : new Date() } })

Which returns me any document in my data where one of its embedded SubItems EndDate is in the past, but how do I alter this to return me the document only if all of its SubItems satisfy the query

In the below example my original query would return both documents but I only want it to return the second one.

{
    "_id" : 1,
    "name" : "item1"
    "SubItems" : [
        {
            "EndDate": ISODate("2016-10-01T00:00:00.000Z"),
        },
        {
            "EndDate": ISODate("2016-04-01T00:00:00.000Z"),
        },
    ]
}
{
    "_id" : 2,
    "name" : "item2"
    "SubItems" : [
        {
            "EndDate": ISODate("2016-02-01T00:00:00.000Z"),
        },
        {
            "EndDate": ISODate("2016-03-01T00:00:00.000Z"),
        },
    ]
}
CeejeeB
  • 3,034
  • 4
  • 25
  • 32

1 Answers1

1

After seeing this question

I managed to achieve what i needed with the following query:

edb.Bookings.find( { "ItineraryItems.EndDate" : { $not : {  $gt : new Date() } } })
Community
  • 1
  • 1
CeejeeB
  • 3,034
  • 4
  • 25
  • 32