gpt4 book ai didi

javascript - 如何检查嵌入文档中的元素是否为空? Node.js 和 Mongodb

转载 作者:行者123 更新时间:2023-12-03 06:11:29 25 4
gpt4 key购买 nike

我想检查嵌入文档中的元素是否为空。例如:

if (files.originalFilename === 'photo1.png') {
user.update(
{
userName: userName
},
{ $set: { "competitorAnalysisPhoto.photo1" : files.path } }
);
console.log('Got photo1!');

// save the user
user.save();
}

目前,如果“photo1”字段为空,我无法更新该字段。如何检查它是否为空?

我尝试使用:

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 } }
);
}

但是,无论photo1存在与否,它始终为null,打印出“undefined”。

最佳答案

您尝试过使用“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();
}

关于javascript - 如何检查嵌入文档中的元素是否为空? Node.js 和 Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com