[英]How to check if elements in Embedded Document is empty or not? Node.js & Mongodb
I want to check if elements in Embedded Document is empty or not. 我想检查Embedded Document中的元素是否为空。 For example: 例如:
if (files.originalFilename === 'photo1.png') {
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } }
);
console.log('Got photo1!');
// save the user
user.save();
}
Currently, I cannot update the field "photo1" if it is empty. 当前,如果字段“ photo1”为空,则无法更新。 How can I check if it is empty or not? 如何检查是否为空?
I try to use: 我尝试使用:
if (user.competitorAnalysisPhoto.photo1 == null) {
console.log('Photo1 is null!');
user.competitorAnalysisPhoto.push({
photo1: files.path
});
} else {
console.log('Photo1 is not null!');
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } }
);
}
However, it is always null no matter photo1 exists or not, printing out "undefined". 但是,无论photo1是否存在,它始终为null,打印出“ undefined”。
Have you tried using 'upsert'? 您是否尝试过使用“ upsert”?
if (files.originalFilename === 'photo1.png') {
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } },
{ upsert: true }
);
console.log('Got photo1!');
// save the user
user.save();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.