I have a query where I need to find a document by ID, I then need to only return items inside of an array that match certain conditions.
In the code below you will see that I need to only "match" one document by ID, I am then putting a "limit" of one (there should only be one). This is were I really get confused...I am then "unwind"ing the fragments field which is the array, inside the array are subdocuments. I need to filter out these subdocuments by the last item of an array inside of it, so I "project" a "slice" of the assignments_array. This is were i only need the documents where the assignments_array is EMPTY, or where assignment_history.to is null
db.getCollection('territories').aggregate([
{
"$match": {
"congregation": ObjectId("5c68c706f1f52047f08862b3")
}
},
{
"$limit": 1
},
{
$unwind: {
path: "$fragments"
}
},
{
$project: {
"fragments.number": 1,
"fragments.assignment_history": {"$slice": ["$fragments.assignment_history", -1]}
}
}
This gets me this result...
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 1,
"assignment_history": [{
"to": ObjectId("5c68c706f1f52047f08862b4"),
"on": 1550370567067
}]
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 2,
"assignment_history": []
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 3,
"assignment_history": [{
"to": null,
"on": 1550370567067
}]
}
}
I need to end up with 2 objects, where assignment_history.to is null, and where assignment_history has no items/length.