簡體   English   中英

如何檢查Embedded Document中的元素是否為空? Node.js和Mongodb

[英]How to check if elements in Embedded Document is empty or not? Node.js & Mongodb

我想檢查Embedded Document中的元素是否為空。 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM