I am trying to figure out how best to find documents that contain an array of sub documents in the array named statusHistory where either of these 2 conditions exist:
- The statusHistory array is empty (no sub docs)
- The statusHistory array only contains any number of sub documents where all the sub documents have a deletedOn property with an ISO date set; stated differently, any and all sub docs must have a deletedOn; these are "soft deletes"
In this example, the query should only return the unit docs for the following matching units:
- 200 (all sub docs have deletedOn)
- 300 (all sub docs have deletedOn)
- 400 (statusHistory array is empty)
- 500 (statusHistory array is empty)
In this example, the following docs shouldn't match:
- 100 (one sub doc exists that doesn't have a deletedOn)
- 600 (not all of the sub docs contain deletedOn, in this case none of them do)
{
"unit":"100",
"statusHistory":[
{
"action":"Move-In",
"deletedOn":ISODate("2017-02-08T18:27:52.938+0000")
},
{
"action":"Assignment"
}
]
},
{
"unit":"200",
"statusHistory":[
{
"action":"Move-In",
"deletedOn":ISODate("2017-06-08T21:27:52.938+0000")
},
{
"action":"Assignment"
"deletedOn":ISODate("2017-05-08T21:27:52.938+0000")
}
]
},
{
"unit":"300",
"statusHistory":[
{
"action":"Move-In",
"deletedOn":ISODate("2017-06-08T21:27:52.938+0000")
},
{
"action":"Assignment"
"deletedOn":ISODate("2017-05-08T21:27:52.938+0000")
},
{
"action":"Move-In",
"deletedOn":ISODate("2017-04-08T21:27:52.938+0000")
},
{
"action":"Assignment"
"deletedOn":ISODate("2017-03-08T21:27:52.938+0000")
}
]
},
{
"unit":"400",
"statusHistory":[]
},
{
"unit":"500",
"statusHistory":[]
},
{
"unit":"600",
"statusHistory":[
{
"action":"Move-In",
},
{
"action":"Assignment"
}
]
}