1
 router.put('/experience/update/:exp_id',
    auth,
    async (req, res) => {

        const {
            title,
            company,
            location,
            from,
            to,
            current,
            description
        } = req.body;

        const newExp = {};
        newExp._id = req.params.exp_id;

        if (title) newExp.title = title;
        if (company) newExp.company = company;
        if (location) newExp.location = location;
        if (from) newExp.from = from;
        if (to) newExp.to = to;
        if (current) newExp.current = current;
        if (description) newExp.description = description;

        try {
            let profile = await Profile.findOne({ user: req.user.id });

            if (profile) {
                //UPDATE Experience
                profile = await Profile.findOneAndUpdate(
                    { user: req.user.id });
                const updateIndex = profile.experience.map(exp => exp._id).indexOf(req.params.exp_id);

                profile.experience[updateIndex] = newExp;
                    console.log('Experience updated!')
            }
            await profile.save();
            res.json(profile);

        } catch (error) {
            console.log(error.message);
            res.status(500).send('Internal Server Error');
        }


    }
)

我正在使用 findOneAndUpdate 方法来更新配置文件猫鼬模型中的体验字段。访问端点后,我输入了更新的详细信息,例如。公司和地点。但我失去了所有其他领域。那么如何仅更新选择字段而其他字段保持不变?

以下是配置文件架构:

    const mongoose = require('mongoose');

const ProfileSchema = new mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'user'
    },
    company: {
        type: String
    },
    website: {
        type: String
    },
    location: {
        type: String
    },
    status: {
        type: String,
        required: true
    },
    skills: {
        type: [String],
        required: true
    },
    bio: {
        type: String
    },
    githubusername: {
        type: String
    },

    experience: [
        {
            title: {
                type: String,
                required: true
            },
            company: {
                type: String,
                required: true
            },
            location: {
                type: String
            },
            from: {
                type: Date,
                required: true
            },
            to: {
                type: Date,
            },
            current: {
                type: Boolean,
                default: false
            },
            description: {
                type: String,
            }
        }
    ],
    education: [
        {
            school: {
                type: String,
                required: true
            },
            degree: {
                type: String,
                required: true
            },
            fieldofstudy: {
                type: String,
                required: true
            },
            from: {
                type: Date,
                required: true
            },
            to: {
                type: Date,
                required: true
            },
            current: {
                type: Boolean,
                default: false
            },
            description: {
                type: String,
            }
        }
    ],
    social: {
        youtube: {
            type: String,

        },
        twitter: {
            type: String,

        },
        facebook: {
            type: String,

        },
        linkedIn: {
            type: String,

        },
        instagram: {
            type: String,

        }
    },
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Profile = mongoose.model('profile', ProfileSchema);
4

1 回答 1

2

您的代码中存在一些问题。

  1. 您只将一个参数传递给findOneAndUpdate. 理想情况下,语法是findOneAndUpdate(filter, update). 所以基本上你需要将更新查询作为第二个参数传递。
profile = await Profile.findOneAndUpdate(
                    { user: req.user.id });
  1. 在下面的代码中,您正在修改配置文件对象并保存它。这不是必需的。这也是你失去领域的原因。
 const updateIndex = profile.experience.map(exp => exp._id).indexOf(req.params.exp_id);

                profile.experience[updateIndex] = newExp;
                    console.log('Experience updated!')
            }
            await profile.save();

解决方案-

我们需要弄清楚 的update部分findOneAndUpdate(filter, update)

这是更新查询 -

db.collection.update({
  "user": "5f96dc85ac5ae03160a024a8",
  "experience._id": "5f9826c3a3fa002ce0f11853"
},
{
  "$set": {
    "experience.$": {
      "current": false,
      "_id": "5f9826c3a3fa002ce0f11853",
      "title": "Senior developer",
      "company": "Morgan Stanley",
      "location": "Pune",
      "from": "2017-04-30T18:30:00.000Z",
      "to": "2020-07-08T18:30:00.000Z",
      "description": "testing"
    }
  }
})

在这里试试

尝试猫鼬的方式:

const filter = { user: req.user.id, "experience._id": req.params.exp_id }
const update = { $set: { "experience.$": newExp } }
profile = await Profile.findOneAndUpdate(filter,update);
于 2020-10-27T14:15:55.463 回答