Use $in and $gte together - Mongodb

Asked

Viewed 18 times

0

I am developing an order system(type Ifood - from deepweb haha) I have 2 models, one of requests(fields: user information and a subdocument with the products) and another of the products.

When the user finalizes the purchase, I wanted to check if it has the quantity that the user ordered (it may be more than one product), and if it has all the products create the order and assume the value of the product (once it has been purchased). But I’m not getting that check.

Product model:

const ProdutoSchema = new mongoose.Schema({
    img_produto: {
        required: true,
        type: String
    },
    nome_produto: {
        trim: true,
        required: true,
        lowercase: true,
        type: String
    },
    preco_produto: {
        required: true, 
        type: Number
    },
    quantidade_produto: {
        required: true,
        type: Number
    }
}, {
    timestamps: true
})

Model request:

const produtosSchema = new mongoose.Schema({
    id_produto: {
        type: String,
        required: true
    },
    qtd_produto: {
        type: Number,
        required: true
    }  
})

const statusSchema = new mongoose.Schema({
    nome: {
        type: String
    }
}, {
    timestamps: true
})

const PedidoSchema = new mongoose.Schema({
    id_usuario: {
        type: String,
        required: true,
    },
    tipo_envio: {
        type: String,
        required: true
    },
    nome_usuario: {
        type: String,
        required: true,
    },
    endereco: {
        type: String,
        required: true
    },
    complemento: {
        type: String,
        required: true
    },
    telefone: {
        type: String,
        required: true
    },
    produtos: [produtosSchema],
    status: [statusSchema]
}, {
    timestamps: true
})

**The way I’m doing: **

const {token, telefone, nome, complemento, endereco, envio, produtos} = req.body

^^ above - take information and products - ex product: [{"_id": 'idDoProduct', "qtd_product": 2}]

 let productData = {ids: [], qtd_produto: []}
            

produtos.map(item => { 
                    productData.ids.push(item._id)

^^ I place all ids in an array (within the loop)-above ^^

productData.qtd_produto.push(Number(item.qtd_produto))

^^ I place all amounts in array (inside the loop)-above ^^

  })
            

Produto.find({_id:{$in: productData.ids}, quantidade_produto: {$in: {$gte: productData.qtd_produto}}}).then(e => {
                

^^ I check if you have this amount of products - with all( the search only with the ids works, but with the quantity_product field does not work) - above ^^

                          res.json(e) 
                    }).

^^ print the product(s) (s) ^^

  • u need. use Aggregate for this.. doing match later can use a filter in a project to solve your problem.. No need to redo the Node to get the final result. Look at the documentation of Mongo is the best and complete for you ententer. https://docs.mongodb.com/v4.2/reference/operator/aggregation-pipeline/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.