Home > Software design >  Find items that are equal to or less than the amount received mongoose
Find items that are equal to or less than the amount received mongoose

Time:02-26

I need to return the elements in which the stock is less than or equal to the quantity that I receive from the request.

In my product document I have a property called stock that I need to compare against the quantity of product that I receive for the request, which is an array with the order information.

Data request: data request

To find the products with the stock equal to or less than the quantity that comes from the request, I am doing the following:

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);
}

Clearly this doesn't work because $lte requires you to pass it an integer and here you're getting an array.

If I run it this way it obviously works:

const product = await Product.find({ stock: { $lte: 20} }).where('_id').in(ids);

But I need to know how to achieve it with the data I receive from the request using the quantity and validate it against the stock.

CodePudding user response:

First of all you are comparing stock in findQuery with an array as you have declared stock as array.

let productHasStock =   async ( req, res, next ) => {

const details = req.body.detail;
let ids = [];
let stock = []; // here stock is array type

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); // here you are comparing array it should be integer.

}

And logically if you have stock=['5','100'] then why you are passing array while comparing stock in db. As you know that larger value in stock is 100 so you should compare only 100 as you only want products that have stock less than equal to 100.

const product = await Product.find({ stock: { $lte: 100} }).where('_id').in(ids);

And you should find the largest one first from the array then pass that to the query.

Or if you want to compare product with id and then you want to compare the specific id product with the stock of that product then thats not the good approach logically you are using. So write an aggregate pipeline in mongoose to find the specific product with id and compare their stock too.

CodePudding user response:

I assume the query is to find stock purchase requests that cannot be completely fulfilled. Each request can be a clause in the find "$or". Of course you will still need to transform the data request into the find query.

db.Product.find({
  // Each data request is part of "$or"
  "$or": [
    {
      "_id": 0,
      "stock": { "$lte": 200 }
    },
    {
      "_id": 2,
      "stock": { "$lte": 100 }
    },
    {
      "_id": 5,
      "stock": { "$lte": 500 }
    },
    {
      "_id": 7,
      "stock": { "$lte": 300 }
    }
  ]
})

Try it on mongoplayground.net.

  • Related