我想使用与其关联的ID更新体验数组中的值

时间:2019-02-04 11:57:29

标签: node.js mongodb api

我正在使用mongoDB和nodeJS开发rest API服务。我在更新Experience数组中的值时遇到问题。我正在共享该数据库的JSON结构,还共享了我已经尝试过的代码。

数据库JSON结构

我需要更新一个体验数组的第一个元素,具有该数组元素ID的状态值将在路径参数上传递。我也尝试过那样。但是什么都没有改变。请帮助我

{
    "_id": {
        "$oid": "5c35c40c9b3feb2ebc89b24a"
    },
    "skills": [
        "JS",
        "React",
        "Bootstrap"
    ],
    "user": {
        "$oid": "5c35c048e0a0f22e9429b145"
    },
    "handle": "dhanush",
    "status": "developer",
    "experience": [
        {
            "current": false,
            "_id": {
                "$oid": "5c36d8294d901d02bc299e06"
            },
            "title": "Developers",
            "company": "Sword Group",
            "location": "Chennai",
            "from": {
                "$date": "2018-02-03T18:30:00.000Z"
            },
            "to": {
                "$date": "2019-12-11T18:30:00.000Z"
            },
            "description": "i started working here for 1 year ago"
        },
        {
            "current": false,
            "_id": {
                "$oid": "5c35f9864d901d02bc299e02"
            },
            "title": "Developers",
            "company": "Sword India",
            "location": "Chennai",
            "from": {
                "$date": "2018-02-03T18:30:00.000Z"
            },
            "to": {
                "$date": "2019-12-11T18:30:00.000Z"
            },
            "description": "i started working here for 1 year ago"
        }
    ],
    "education": [
        {
            "current": false,
            "_id": {
                "$oid": "5c36d8114d901d02bc299e05"
            },
            "school": "Pondicherry Engineering College",
            "degree": "btech",
            "fieldofstudy": "Mechanicalll",
            "from": {
                "$date": "2013-01-02T00:00:00.000Z"
            },
            "to": {
                "$date": "2017-01-01T00:00:00.000Z"
            },
            "description": "I studied in Pondicherry"
        }
    ],
    "date": {
        "$date": "2019-01-09T09:51:08.839Z"
    },
    "__v": 16,
    "company": "Sword India",
    "social": {
        "twitter": "http://twitter.com/dhanush",
        "facebook": "http://facebook.com/dhanush"
    },
    "website": "http://www.sword-india.com"
}

我尝试过这样。

router.post(
'/experience',
(req, res) => {

Profile.findOneAndUpdate({ user: req.user.id }).then(profile => {
const newExp = {
title: req.body.title,
company: req.body.company,
location: req.body.location,
from: req.body.from,
to: req.body.to,
current: req.body.current,
description: req.body.description
};

// Add to exp array


profile.save().then(profile => res.json(profile));
});
}
);

1/5

0 个答案:

没有答案