首页 > 解决方案 > 如何将数据库中价格的字符串格式更改为 mongodb 中的整数格式?

问题描述

这是我的输入表单。在这里,价格标签在Database中保存为字符串。如何将其更改为整数格式。我正在使用 mongodb 3.2 版本

  <section>
    <div class="container mt-4">
    <div class="row">

        <div class="col-md-6">
            <h2 class="text-center">Add Product</h2>
            <form action="/admin/add-product" method="POST" enctype="multipart/form-data">
                <label for="">Name</label>
                <input type="text" name="Name" class="form-control">

                <label for="">Category</label>
                <input type="text" name="Category" class="form-control">

                <label for="">Price</label>
                <input type="number" name="Price" class="form-control">

                <label for="">Description</label>
                <input type="text" name="Description" class="form-control">

                <label for="">Image</label>
                <input type="file" name="Image" class="form-control">

                <button type="submit" class="btn btn-success mt-4">Submit</button>
            </form>
        </div>

    </div>
    </div>
</section>

添加到购物车

    addProduct:(product,callback)=>{


    db.get().collection("product").insertOne(product).then((data)=>{
        console.log(data)
        
        callback(data.ops[0]._id)
    })
    

}

标签: node.jsmongodb

解决方案


在将产品添加到数据库之前将价格更改为整数

     let Price=parseInt(req.body.Price)
      req.body.Price=Price

然后将产品添加到 mongodb


推荐阅读