我是node和mongo的新手,我不知道该怎么做。{
"_id": "5b6af36fdca5531d140c4b36",
"contact": 123456,
"email": "John@xyz.com",
"u_password": "user",
"u_name": "John Doe",
"u_id": 1,
"__v": 0,
"address": [
{
"street": "xyz",
"city": "abc",
"state": "pqr"
}
]
}
这是我现有的架构,我希望输出如下
{
"_id": "5b6af36fdca5531d140c4b36",
"contact": 123456,
"email": "John@xyz.com",
"u_password": "user",
"u_name": "John Doe",
"u_id": 1,
"__v": 0,
"address": [
{
"street": "xyz",
"city": "abc",
"state": "pqr"
},
{
"street" : "aaa",
"city" : "bbb",
"state" : "ccc"
}
]
}
使用节点,我已经创建了现有的模式。现在,我想向此现有模式添加数据。我想要的输出在上面给出。
api.put('/address/add/:id', (req, res) => {
User.findById( req.params.id , (err, user) => {
if (err) {
res.send(err);
}
const u = new user();
var address = [
{
street : req.body.street,
city : req.body.city,
state : req.body.state
}
];
u.address = req.body.address;
u.save((err) => {
if (err){
res.send(err);
}
user.address.push(u);
res.json({ message: 'address added'});
});
});
});
这是我的代码