我希望能够在Mongodb中插入两个不同集合中的两个文档

时间:2019-06-28 08:34:14

标签: node.js mongodb

我希望能够在Mongodb中插入两个不同集合中的两个文档,我尝试了几种形式,但找不到键。另外,我从一个使用jquery自动完成的字段中收集了其中一个值,并将其保存在mongodb中时,结果始终为null。

我使用“ mongodb”:“ ^ 3.2.7”,节点为6.9

router.post("/users", (request, response) => {

    let myobj = {
      first_name: request.body.first_name, 
      last_name: request.body.last_name,
      birth_date: request.body.birth_date,
      nationality: request.body.nationality,
      weight: request.body.weight,
      belt: request.body.belt,
      email: request.body.email,
      isAdmin: false,
      pass: md5("alumno")
    };
    global.dbo.collection("alumnos").insertOne(myobj, function(err, res) {
      if (err) throw err;

    });

    let myStats = {
      userId: { myobj: { $in: [mongo.ObjectID(_id)] } },
      Cortesia: request.body.choice1,
      Integridad: request.body.choice2,
      Perseverancia: request.body.choice3,
      Auto_Control: request.body.choice4,
      Espiritu_Indomable: request.body.choice5,
      Fuerza: request.body.choice6,
      Equilibrio: request.body.choice7,
      Respiracion: request.body.choice8,
      Tecnica: request.body.choice9,
      Agilidad: request.body.choice10
    };

    global.dbo
      .collection("estadisticas")
      .insertOne(myStats, function(err, res) {
        if (err) throw err;
        response.status(201).send(res.ops[0]); 
      });
  }
});



    const token = sessionStorage.getItem("token");
    let decode = jwt_decode(token)

    if (decode.isAdmin) {
        function newUser() {
            const first_nameValue = 
              document.getElementById("first_name").value;
            const last_nameValue = 
              document.getElementById("last_name").value;
            const birth_dateValue = 
              document.getElementById("birth_date").value;
            const nationalityValue = 
               document.getElementById("nationality").value;
            const weightValue = document.getElementById("weight").value;
            const beltValue = document.getElementById("belt").value;
            const emailValue = document.getElementById("email").value;


            const CortesiaValue = document.getElementById("choice1").value;
            const IntegridadValue = document.getElementById("choice2").value;
            const PerseveranciaValue = 
              document.getElementById("choice3").value;
            const Auto_ControlValue = 
              document.getElementById("choice4").value;
            const Espiritu_IndomableValue = 
              document.getElementById("choice5").value;
            const FuerzaValue = document.getElementById("choice6").value;
            const EquilibrioValue = document.getElementById("choice7").value;
            const RespiracionValue = 
              document.getElementById("choice8").value;
            const TecnicaValue = document.getElementById("choice9").value;
            const AgilidadValue = document.getElementById("choice10").value;

            const path = location.pathname;
            const id = path.split(["/"]).slice(-1)[0];

            fetch("../api/users/", { 
                method: "POST",
                headers: { 
                    "Content-Type": "application/json"
                },
                body: JSON.stringify({
                    first_name: first_nameValue,
                    last_name: last_nameValue,
                    birth_date: birth_dateValue,
                    nationality: nationalityValue,
                    weight: weightValue,
                    belt: beltValue,
                    email: emailValue,
                    isAdmin: false,
                    Cortesia: CortesiaValue,
                    Integridad: IntegridadValue,
                    Perseverancia: PerseveranciaValue,
                    Auto_Control: Auto_ControlValue,
                    Espiritu_Indomable: Espiritu_IndomableValue,
                    Fuerza: FuerzaValue,
                    Equilibrio: EquilibrioValue,
                    Respiracion: RespiracionValue,
                    Tecnica: TecnicaValue,
                    Agilidad: AgilidadValue
                })
            }).then(response => {
                if (response.ok) {
                    location.href = "/users";
                }
            })
        }

    }

仅保存来自myobj的数据,而不保存mystats的数据,当我收集mystats的数据时,也仅保存为空。

0 个答案:

没有答案