更新子子文档不起作用
Updating sub sub documents not working
我有一个应用程序,我试图在其中向用户 "profile" 文档添加联系人记录,其中每条记录都有 arrays/subdocuments 多个 phone 号码和电子邮件地址。
因此配置文件架构如下所示:
var ContactSchema = require('./contact');
var UserSchema = new Schema({
first_name:{
type: String
},
last_name:{
type: String
},
contacts:[ContactSchema]
});
ContactSchema 如下所示:
var ContactSchema = new Schema({
contact_name:{
type: String
},
contact_age:{
type: String
},
phones:[{
phone_number:{type: String},
phone_type:{type: String}
}],
emails:[{
email_address:{type: String},
email_type:{type: String}
}]
});
我简化了上面的模式,但它们本质上代表了我正在处理的结构。
我的挑战是 ExpressJs/Mongoose API,我想在其中传递对象并更新子文档。
这是我认为可以完成的方式,但这不起作用:
var express = require('express');
var router = express.Router();
var Profile = require('../models/profile');
var vCard = require('vcards-js');
router.route('/addcontact/:ownerId')
.put(function(req, res){
Profile.findOne({'owner_id':req.params.ownerId}, function(err, profile){
if(err)
res.send(err);
profile.contacts.push({
first_name : req.body.first_name,
last_name : req.body.last_name
})
req.body.phones.forEach(function(phone, index, arr){
profile.contacts.phones.push({
phone_type:phone.phone_type,
phone_number:phone.phone_number
})
})
req.body.emails.forEach(function(email, index, arr){
profile.contacts.emails.push({
email_type:email.email_type,
email_address:email.email_address
})
})
profile.save(function(err){
if(err)
res.send(err);
res.json(profile);
})
});
});
module.exports = router;
所以澄清一下,我有一个现有的 "User Profile" 记录。
用户模式有一个 "contacts" 数组来保存多个联系人。
每个联系人都有 phone 和电子邮件数组,因此每个联系人记录可以包含多个电子邮件和 phone 号码。
我正在尝试做的是.PUT 联系人记录并更新用户配置文件。
感谢任何明智的建议!!
var phonesArray = req.body.phones;
var emailsArray = req.body.emails;
Profile.findOneAndUpdate(
{'owner_id':req.params.ownerId}
{phones: {$push: {$each: phonesArray}}, emails: {$push: {$each: emailsArray }}}, function(err, profile){
if(err)
res.send(err);
console.log(profile);
}
您的 User
型号似乎没有 owner_id
。假设这里遗漏了它,并且您想为现有用户添加新联系人。
'use strict';
var express = require('express');
var router = express.Router();
var Profile = require('../models/profile');
router.route('/addcontact/:ownerId').put(function (req, res) {
var phones = req.body.phones;
console.log('phones', phones);
var emails = req.body.emails;
console.log('emails', emails);
var contacts = {
contact_name: req.body.first_name,
contact_age: '25',
emails: emails,
phones: phones
};
Profile.findOneAndUpdate({
owner_id: req.params.ownerId
}, {
$push: {
contacts: contacts
}
}, {
new: true
}, (err, profile) => {
if (err) {
return res.send(err);
}
return res.json(profile);
});
});
module.exports = router;
而且,我对您的代码所做的一个小改动是在发送响应时添加 return
以避免进一步执行代码。
我有一个应用程序,我试图在其中向用户 "profile" 文档添加联系人记录,其中每条记录都有 arrays/subdocuments 多个 phone 号码和电子邮件地址。
因此配置文件架构如下所示:
var ContactSchema = require('./contact');
var UserSchema = new Schema({
first_name:{
type: String
},
last_name:{
type: String
},
contacts:[ContactSchema]
});
ContactSchema 如下所示:
var ContactSchema = new Schema({
contact_name:{
type: String
},
contact_age:{
type: String
},
phones:[{
phone_number:{type: String},
phone_type:{type: String}
}],
emails:[{
email_address:{type: String},
email_type:{type: String}
}]
});
我简化了上面的模式,但它们本质上代表了我正在处理的结构。
我的挑战是 ExpressJs/Mongoose API,我想在其中传递对象并更新子文档。
这是我认为可以完成的方式,但这不起作用:
var express = require('express');
var router = express.Router();
var Profile = require('../models/profile');
var vCard = require('vcards-js');
router.route('/addcontact/:ownerId')
.put(function(req, res){
Profile.findOne({'owner_id':req.params.ownerId}, function(err, profile){
if(err)
res.send(err);
profile.contacts.push({
first_name : req.body.first_name,
last_name : req.body.last_name
})
req.body.phones.forEach(function(phone, index, arr){
profile.contacts.phones.push({
phone_type:phone.phone_type,
phone_number:phone.phone_number
})
})
req.body.emails.forEach(function(email, index, arr){
profile.contacts.emails.push({
email_type:email.email_type,
email_address:email.email_address
})
})
profile.save(function(err){
if(err)
res.send(err);
res.json(profile);
})
});
});
module.exports = router;
所以澄清一下,我有一个现有的 "User Profile" 记录。 用户模式有一个 "contacts" 数组来保存多个联系人。 每个联系人都有 phone 和电子邮件数组,因此每个联系人记录可以包含多个电子邮件和 phone 号码。
我正在尝试做的是.PUT 联系人记录并更新用户配置文件。
感谢任何明智的建议!!
var phonesArray = req.body.phones;
var emailsArray = req.body.emails;
Profile.findOneAndUpdate(
{'owner_id':req.params.ownerId}
{phones: {$push: {$each: phonesArray}}, emails: {$push: {$each: emailsArray }}}, function(err, profile){
if(err)
res.send(err);
console.log(profile);
}
您的 User
型号似乎没有 owner_id
。假设这里遗漏了它,并且您想为现有用户添加新联系人。
'use strict';
var express = require('express');
var router = express.Router();
var Profile = require('../models/profile');
router.route('/addcontact/:ownerId').put(function (req, res) {
var phones = req.body.phones;
console.log('phones', phones);
var emails = req.body.emails;
console.log('emails', emails);
var contacts = {
contact_name: req.body.first_name,
contact_age: '25',
emails: emails,
phones: phones
};
Profile.findOneAndUpdate({
owner_id: req.params.ownerId
}, {
$push: {
contacts: contacts
}
}, {
new: true
}, (err, profile) => {
if (err) {
return res.send(err);
}
return res.json(profile);
});
});
module.exports = router;
而且,我对您的代码所做的一个小改动是在发送响应时添加 return
以避免进一步执行代码。