简体   繁体   English

使用NodeJS和Mongoose将数据插入架构数组

[英]Insert data into array of schema with NodeJS and mongoose

I have the next Schema: 我有下一个模式:

'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = Schema({
  id: String,
  name: [{
    name: String,
    surname1: String,
    surname2: String,
  }]
});

module.exports = mongoose.model('User', userSchema);

And I created the next function: 我创建了下一个函数:

module.exports.signUp = function(req,res){
  console.log('POST signUp');
  let user = new User();

  for(var key in req.body) {
    console.log(key);
    switch (key) {
      case 'name[name]':
        user.name.push({name:"name"});
      break;
      case 'name[surname1]':
        user.name.push({surname1:"surname1"});
      break;
      case 'name[surname2]':
        user.name.push({surname2:"surname2"});
      break;
    }
  }         
  res.status(202).send({message: user});
}

I need the next results: 我需要下一个结果:

{
    "message": {
        "_id": "5b61e242c4874a045dd4185a",
        "name": [
            {
                "name": "name",
                "surname1": "surname1",
                "surname2": "surname2"
            }
        ]
    }
}

And I get: 我得到:

{
    "message": {
        "_id": "5b61e242c4874a045dd4185a",
        "name": [
            {
                "_id": "5b61e242c4874a045dd4185b",
                "name": "name"
            },
            {
                "_id": "5b61e242c4874a045dd4185c",
                "surname1": "surname1"
            },
            {
                "_id": "5b61e242c4874a045dd4185d",
                "surname2": "surname2"
            }
        ]
    }
}

It generates multiples _id and different JSON format. 它生成多个_id和不同的JSON格式。

I tried with user.name[0]="name", user.name.name="name" but It doesn't work... When I tried user.name = {"name":"name"} works, but after that I put user.name = {"surname1":"surname1"} and the name doesn't exists. 我尝试使用user.name[0]="name", user.name.name="name"但是它不起作用...当我尝试user.name = {"name":"name"} ,但是之后,我输入了user.name = {"surname1":"surname1"} ,该名称不存在。

Can you help me please? 你能帮我吗? Thank you! 谢谢!

You're looping through req.body and pushing a key each time. 您正在遍历req.body并每次按一个键。 You need to be doing it all at once. 您需要一次完成所有操作。 Something like 就像是

const name={};
if ('name[name]' in req.body){name.name=req.body['name[name]'];}
if ('name[surname1]' in req.body){name.surname1=req.body['name[surname1]'];}
if ('name[surname2]' in req.body){name.surname2=req.body['name[surname2]'];}
user.name.push(name);

Each entry in the name array will have a separate id though because they're declared as a subdocument in Mongo. 尽管名称数组中的每个条目都会有一个单独的ID,因为在Mongo中它们被声明为子文档。 You'll still be able to access the user by its id, but you'll also have access to each name object individually. 您仍然可以通过其ID访问该用户,但是您还可以分别访问每个名称对象。

If each user just needs one name though, you can simplify the schema to 如果每个用户只需要一个名称,则可以将架构简化为

const userSchema = Schema({
  id: String,
  name: {
    name: String,
    surname1: String,
    surname2: String,
  }
});

and then when you have made the name object do 然后当您使名称对象

user.name=name;

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

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