I’m trying to query for products that are still on sale using this schema and filter:
// Model
const ProductSchema = new Schema({
sale: {
until: Schema.Types.Date
}
})
const Products = model('product', ProductSchema)
// Document
{
sale: {
until: "2024-08-18T05:59:00.000Z"
}
}
// Query
Products.find({ 'sale.until': { $gte: new Date() } })
This always gives me an empty array in Mongoose. If I console log the filter and then paste it into Atlas however, it works just fine. What am I missing from this?
For reference, Mongoose is able to query that field just fine; if I write { 'sale.until': { $exists: false } }
, it will correctly give me documents that don’t have this sub-property.