Javascript 在mongoDB中搜索集合时,如何接受空数据

Javascript 在mongoDB中搜索集合时,如何接受空数据,javascript,node.js,mongodb,crm,Javascript,Node.js,Mongodb,Crm,我目前正在建立一个非常小的CRM的做法,我正试图使一个网页,你可以搜索配置文件。我希望用户输入他所知道的关于他正在查找的配置文件的所有数据,但不允许他填写每个输入。当我试图从MongoDB中获取数据时,没有1个字段,它返回null,对此我能做些什么 name: String, email: String, date: String, notes: String }) module.exports = mongoose.model('profiles', crea

我目前正在建立一个非常小的CRM的做法,我正试图使一个网页,你可以搜索配置文件。我希望用户输入他所知道的关于他正在查找的配置文件的所有数据,但不允许他填写每个输入。当我试图从MongoDB中获取数据时,没有1个字段,它返回null,对此我能做些什么

    name: String,
    email: String,
    date: String,
    notes: String
})

module.exports = mongoose.model('profiles', createProfile)```

this is the schema

    await mongo().then( async (mongoose) => {
        try {
            console.log('Finding data from MongoDB')
            
            const result = await createProfile.findOne({
                name: req.body.name,
                email: req.body.email,
                notes: req.body.notes

            })
            console.log(result)
            res.redirect('/find')
        } catch {
            console.log('Can not connect')
        } finally {
            mongoose.connection.close()
        }
    })
this is the operation

仅包括用户提供值的键。

我正在进行搜索确定,因此,
名称
电子邮件
、或
注释
可能未定义的问题也是如此?它们是在数据库中定义的吗?问题是它们不能为null,因此您必须回答操作中的所有输入才能获得objectRight,因为您将它们包括在查询中。如果它们为null,并且您不想查找该键具有null值的元素,请不要将其包含在queryi know中,但我希望将其设置为可选,以便您仍然可以在不使用字段或
var query = {};

['name', 'email', 'notes'].forEach(key => {
    if (req.body[key]) {
        query[key] = req.body[key];
    }
});

const result = await createProfile.findOne(query);