I have documents with a history. I want to find documents with a special history entry. History-Element:
{
name:"added",
date: Isodate
}
The query should get all documents where name is "added" and the date is lower than a reference. In case of having a date that is higher, the document should be skipped. A document can have multiple "added" events
Here's my query, to find all documents with added history elements in the range of 7 days, if one element is lower than 7 days, the document should not be displayed:
var expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() - 7);
var query = {
$and: [
{
history: {
$elemMatch:{
event: "added",
date: {
$not: {
$gte: expiryDate.toISOString()
},
}
}
}
},
{
history: {
$elemMatch: {
event: "added",
date: {
$lt: expiryDate.toISOString()
}
}
}
}
]
}
Here's the call:
self.db.items.find(query).toArray(function (err, items) {
if(items)
console.log(items.length);
});
The problem is, that only the $lte part works. If there's a date greater than the reference it will be outputted though.