I am looking to retrieve elements where the stock is less than or equal to the quantity received from a request.
Within my product document, there is a property called stock that needs to be compared with the quantity of the products in the request, which are stored in an array containing order information.
Data request: https://i.sstatic.net/CmMg1.png
To identify products with stock quantities equal to or lower than those requested, I am implementing the following logic:
let productHasStock = async ( req, res, next ) => {
const details = req.body.detail;
let ids = [];
let stock = [];
const products = details.map( detail => {
ids.push(detail._id),
stock.push(detail.quantity)
});
const product = await Product.find({ stock: { $lte: stock } }).where('_id').in(ids);
}
However, using $lte
with an array as input does not work since it requires an integer value.
The scenario works if approached this way:
const product = await Product.find({ stock: { $lte: 20} }).where('_id').in(ids);
Yet, I am seeking guidance on how to carry out this operation using the quantity data from the request and properly validating it against the available stock.