{
"_id": ObjectId("53ab1d2c256072374a5cc63f"),
"title": "10% Off",
"endDate": "2015-05-08",
"limit" : "limited",
"redemptions": [
"1f7f5f96be3a",
"kf40vksk03ps"
]
}
{
"_id": ObjectId("53ab1d2c25607sfdgs74a5cc63f"),
"title": "20% Off",
"endDate": "2015-06-07",
"limit" : "unlimited",
"redemptions": [
"1f7f5f96be3a",
"1f7f5f96be3a",
"kf40vksk03ps"
]
}
Story: a person can redeem a coupon 2 times. After 2 times, don't return it. How can I check that a value appears less than 2 times??
Wish it was as easy as:
{ 'redemptions' : { $exists: true }, $where : 'this.redemptions.$.1f7f5f96be3a.length < 2' }
How can I get a count for how many times a specific value is in an array and compare on that?
Edit
So to add some fun. I updated my schema, so I need to put that into a conditional. If limit = 'unlimited' { return record } if limit = 'limited' { return only if array has less than 2 values = '1f7f5f96be3a'