Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
Find items that are equal to or less than the amount received mongoose

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda