Javascript 检查文档是否已经存在,如果是,则更新,否则创建新的Mongoose

Javascript 检查文档是否已经存在,如果是,则更新,否则创建新的Mongoose,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我有一个应用程序,我正在尝试检查一个用户ID为的文档是否已经存在,如果存在,则使用请求中发送的数据更新现有文档。如果不存在,则创建一个新文档 到目前为止,我已经这样做了: Bio.findOne({userID: req.body.userID}, function(err, bio) { if (err) { console.log("The error while updating colleciton is " + err); }

我有一个应用程序,我正在尝试检查一个用户ID为的文档是否已经存在,如果存在,则使用请求中发送的数据更新现有文档。如果不存在,则创建一个新文档

到目前为止,我已经这样做了:

Bio.findOne({userID: req.body.userID}, function(err, bio) {
        if (err) {
            console.log("The error while updating colleciton is " + err);
        }
        // if already exists then update it
        if (bio) {
            console.log("Bio document for user already exists!");
            bio.userBios = {
                $push: {
                    background: req.body.userBios.background,
                    experience: req.body.userBios.experience,
                    skills: req.body.userBios.skills,
                    bioForSector: req.body.userBios.bioForSector
                }
            }
            bio.save(function(err) {
                if (err)
                    throw err
                return (null, bio)
            })
        }
        //if does not exist then create new 
        if (!bio) {
            console.log("Bio document for user does NOT exist! creating new");
            var bio = new Bio();

            bio.firstName = req.body.firstName;
            bio.lastName = req.body.lastName;
            bio.jobTitle = req.body.jobTitle;
            bio.userID = req.body.userID;
            bio.userBios = {
                background: req.body.userBios.background,
                experience: req.body.userBios.experience,
                skills: req.body.userBios.skills,
                bioForSector: req.body.userBios.bioForSector
            };

            // save the bio
            bio.save(function(err) {
                if (err)
                    throw err;
                return (null, bio);
            });
        }
    });
// define the schema for our bios model
 var biosSchema = mongoose.Schema({
     firstName: String,
     lastName: String,
     jobTitle: String,
     userID: String,
     userBios: [{bioForSector: String, background: String, skills: [String], experience: {}}]
     });
问题:

Bio.findOne({userID: req.body.userID}, function(err, bio) {
        if (err) {
            console.log("The error while updating colleciton is " + err);
        }
        // if already exists then update it
        if (bio) {
            console.log("Bio document for user already exists!");
            bio.userBios = {
                $push: {
                    background: req.body.userBios.background,
                    experience: req.body.userBios.experience,
                    skills: req.body.userBios.skills,
                    bioForSector: req.body.userBios.bioForSector
                }
            }
            bio.save(function(err) {
                if (err)
                    throw err
                return (null, bio)
            })
        }
        //if does not exist then create new 
        if (!bio) {
            console.log("Bio document for user does NOT exist! creating new");
            var bio = new Bio();

            bio.firstName = req.body.firstName;
            bio.lastName = req.body.lastName;
            bio.jobTitle = req.body.jobTitle;
            bio.userID = req.body.userID;
            bio.userBios = {
                background: req.body.userBios.background,
                experience: req.body.userBios.experience,
                skills: req.body.userBios.skills,
                bioForSector: req.body.userBios.bioForSector
            };

            // save the bio
            bio.save(function(err) {
                if (err)
                    throw err;
                return (null, bio);
            });
        }
    });
// define the schema for our bios model
 var biosSchema = mongoose.Schema({
     firstName: String,
     lastName: String,
     jobTitle: String,
     userID: String,
     userBios: [{bioForSector: String, background: String, skills: [String], experience: {}}]
     });
我可以成功地创建一个新文档,但当我尝试更新它时,它会覆盖整个
userBios
数组,而不是更新它

我的文档架构:

Bio.findOne({userID: req.body.userID}, function(err, bio) {
        if (err) {
            console.log("The error while updating colleciton is " + err);
        }
        // if already exists then update it
        if (bio) {
            console.log("Bio document for user already exists!");
            bio.userBios = {
                $push: {
                    background: req.body.userBios.background,
                    experience: req.body.userBios.experience,
                    skills: req.body.userBios.skills,
                    bioForSector: req.body.userBios.bioForSector
                }
            }
            bio.save(function(err) {
                if (err)
                    throw err
                return (null, bio)
            })
        }
        //if does not exist then create new 
        if (!bio) {
            console.log("Bio document for user does NOT exist! creating new");
            var bio = new Bio();

            bio.firstName = req.body.firstName;
            bio.lastName = req.body.lastName;
            bio.jobTitle = req.body.jobTitle;
            bio.userID = req.body.userID;
            bio.userBios = {
                background: req.body.userBios.background,
                experience: req.body.userBios.experience,
                skills: req.body.userBios.skills,
                bioForSector: req.body.userBios.bioForSector
            };

            // save the bio
            bio.save(function(err) {
                if (err)
                    throw err;
                return (null, bio);
            });
        }
    });
// define the schema for our bios model
 var biosSchema = mongoose.Schema({
     firstName: String,
     lastName: String,
     jobTitle: String,
     userID: String,
     userBios: [{bioForSector: String, background: String, skills: [String], experience: {}}]
     });

我做错了什么?有人能帮忙吗?非常感谢

正如Alexander Mac在评论中所说,您自己正在覆盖数组。如果文档存在,请尝试:

if (bio) {
    console.log("Bio document for user already exists!");
    bio.userBios.push({
        background: req.body.userBios.background,
        experience: req.body.userBios.experience,
        skills: req.body.userBios.skills,
        bioForSector: req.body.userBios.bioForSector
    });
    bio.save(function(err) {
        if (err)
            throw err
        return (null, bio)
    });
}

这样,您就可以将它推送到现有的数组中,而不是创建一个新的数组。

覆盖整个文档是什么意思?哪个条件得到满足?@RayonDabre当用户创建新文档时,它包含第一个bio的
背景、技能、经验
,但当用户更新它时,而不是推送新的
背景、技能,将
作为架构中的
userBios
数组中的新对象体验,它将使用所有空数据覆盖上一个对象。这意味着数据库中的
背景、技能、经验
变量更改为
null
。这有帮助吗?@RayonDabre我改变了我的措辞,我的意思是它覆盖了整个
userbios
数组,而不是通过将一个新对象推入数组来更新它。只需将一个新项目推入
bio.userbios
bio.userbios.push(userBiosObject)
。并在
bio.save()
@AlexanderMac中使用回电或承诺谢谢你的小费,这很有效!!!