mongodb创建新记录 - 如果字段不存在

时间:2017-08-27 16:26:10

标签: mongodb

我正在处理从学生表到"文件"的数据。表
目前我的条件是doc.isDeleted == true?true:false
如何添加检查以查看isDeleted是否存在,如果不存在则将其视为false?

 db.students.find({"studentPicture":{$ne:null}}, { studentPicture:1,studentId:1,isDeleted:1,_id:0}).forEach(function (doc) {

db.documents.insertOne(
            { 
                "className" : "com.model.ModuleDocument", 
                "documentId" : doc.studentPicture, 
                "studentId":doc.studentId,
                "documentTypeStr" : "LICENCE",      
                 "isDeleted": doc.isDeleted==true?true:false
                });

});

这似乎错误/不会编译

"isDeleted": if (doc.isDeleted == null){false} else if(doc.isDeleted==true){true} else { false }

1 个答案:

答案 0 :(得分:2)

简单:

"isDeleted": doc.isDeleted === null ? false : (doc.isDeleted === true ? true : false)