简体   繁体   English

我应该如何使用表单更新一个多个值?

[英]How should I UpdateOne multiple values with a form?

This is where I want to try to edit every single value, using a single bottom to send all the data.这是我想尝试编辑每个值的地方,使用单个底部发送所有数据。

在此处输入图像描述 ack.imgur.com/S5jg1.png ack.imgur.com/S5jg1.png

This is my form.这是我的表格。 My idea was to put a hidden input value that every document has, DescripcionBusqueda.我的想法是放置一个每个文档都有的隐藏输入值,DescriptionBusqueda。

<form
  action="/EdicionMaterialesPost"
  method="POST"
  enctype="multipart/form-data"
>
  <% for (var a = 0; a < materiales.length; a++) { %>
  <tr>
    <th>
      <%=materiales[a].Descripcion%>
      <input
        type="text"
        class="form-control"
        placeholder="Descripcion"
        id="Descripcion"
        name="Descripcion"
      />
      <input
        type="hidden"
        class="form-control"
        id="DescripcionBusqueda"
        name="DescripcionBusqueda"
        value="<%=materiales[a].Descripcion%>"
      />
    </th>
    <th>
      <%=materiales[a].Codigo%>
      <input
        type="text"
        class="form-control"
        placeholder="Codigo"
        id="Codigo"
        name="Codigo"
      />
    </th>
    <th>
      <%=materiales[a].Unidad%>
      <input
        type="text"
        class="form-control"
        placeholder="Unidad"
        id="Unidad"
        name="Unidad"
      />
    </th>
    <th>
      <%=materiales[a].PrecioUnitario%>
      <input
        type="number"
        class="form-control"
        placeholder="PrecioUnitario"
        id="PrecioUnitario"
        name="PrecioUnitario"
      />
    </th>
    <th>
      <%=materiales[a].Familia%>
      <input
        type="text"
        class="form-control"
        placeholder="Familia"
        id="Familia"
        name="Familia"
      />
    </th>
    <th>
      <%=materiales[a].SubFam%>
      <input
        type="text"
        class="form-control"
        placeholder="SubFam"
        id="SubFam"
        name="SubFam"
      />
    </th>
  </tr>
  <% } %>
  <button type="submit" id="sendMessageButton">Guardar Cambios</button>
</form>

And here is where I try to UpdateOne, I was thinking about using a for "With the "descriptionBusqueda" as index 0, will only UpdateOne documents (req.body.Myinputname) with index 0".这是我尝试 UpdateOne 的地方,我正在考虑使用“将“descriptionBusqueda”作为索引 0,只会使用索引 0 的 UpdateOne 文档(req.body.Myinputname)”。 And input without value won't be changed... but everything becomes chaos.没有价值的输入不会改变......但一切都变得混乱。

Can I have some help with my logic?我的逻辑能得到一些帮助吗?

const material = require('../models/materiales.js');
const path = require('path');

module.exports = async (req, res) => {
  for (i = 0; i < req.body.DescripcionBusqueda.length; i++) {
    await material.updateOne(
      { Descripcion: req.body.DescripcionBusqueda[i] },
      {
        $set: {
          Descripcion: req.body.Descripcion[i],
          Codigo: req.body.Codigo[i],
          Unidad: req.body.Unidad[i],
          PrecioUnitario: req.body.PrecioUnitario[i],
          Familia: req.body.Familia[i],
          SubFam: req.body.SubFam[i],
        },
      },
      { upsert: false }
    );
  }

  res.redirect('/');
};

Example of my doc我的文档示例

_id ObjectoId('6396d947f3a8a9fa8c3c7727')
Descripcion:"Angulo 1/8" x 1 1/4" de 6.10 mts. (1.50 kg/mt)"
Codigo:"AA-03"
Unidad:"Pza."
PrecioUnitario:260.84
Familia:"Aceros"
SubFam:"Angulos"
__v:0

You will have to check the form for changed data and programmatically construct the $set component of the update.您将必须检查表单中是否有更改的数据,并以编程方式构建更新的$set组件。 Your logic with the iteration is correct;您的迭代逻辑是正确的; there is no way (with update() ) to update a bunch of different docs in MongoDB with different variations of $set in one call.没有办法(使用update() )在一次调用中使用$set的不同变体更新 MongoDB 中的一堆不同文档。 Your logic will end up something like the following.你的逻辑最终会像下面这样。 If you don't have marker vars like DescripcionCHANGED that is OK;如果您没有像DescripcionCHANGED这样的标记变量,那也没关系; you will just have to compare before-and-after versions using your hidden vars.您只需要使用hidden的变量比较前后版本。


    for(i=0; i<req.body.DescripcionBusqueda.length;i++){
        var sdoc = {};
        if(req.body.DescripcionCHANGED[i]) {
          sdoc['Descripcion'] = req.body.Descripcion[i]
        }
        if(req.body.CodigoCHANGED[i]) {
          sdoc['Codigo'] = req.body.Codigo[i]
        }
        if(req.body.UnidadCHANGED[i]) {
          sdoc['Unidad'] = req.body.Unidad[i]
        }
        //  etc. etc.....

        material.updateOne(
                {Descripcion:req.body.DescripcionBusqueda[i]},
                {$set: sdoc},
                { upsert: false });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM