如何使用findOneAndUpdate在数组内添加信息而不删除以前包含的信息

时间:2019-05-21 23:50:23

标签: javascript node.js

有人知道我如何在不“替换”它包含的所有信息的情况下将对象添加到名为worker_info的数组中吗? (我正在学习平均堆栈)我有这段代码,但是它的作用是替换所有信息并擦除我已经拥有的其他数据。有了我得到的代码,总结一下我想要的是将worker_info数组添加到worker对象中而不删除其余部分,如果有人可以帮助我,我将不胜感激,任何建议都将非常有用,谢谢

我尝试过

 User.findOneAndUpdate(serviceId, { "$push": { services: { worker_info: worker } } }, { new: true }, (err, us) => {

res.status(200).send(us);
                    });

我期望这样的事情

{
    "services": [
        {
            "worker_info": [],
            "_id": "5ce485a3288efe1834d4d71c",
            "user_address": "dbarva252",
            "description_service": "descripcion servicio",
            "skill_type": "wasting time",
            "amount_money": 0
        },
        {
            "worker_info": [{
                "_id": "5ce4867e288efe1834d4d71e",
                "name": "Trabajador",
                "surname": "arreglador",
                "identification": "123457669",
                "birthdate": "1 febrero 1999",
                "skills": "time waster fixer",
                "image": null,
                "__v": 0
            }],
            "_id": "5ce485c5288efe1834d4d71d",
            "user_address": "esta es una direccion",
            "description_service": "descripcion servicio 2",
            "skill_type": "wasting time part 2",
            "amount_money": 0
        }
    ],
    "_id": "5ce48569288efe1834d4d71a",
    "user": "usuario123",
    "name": "mauricio",
    "surname": "barva",
    "identification": "123123",
    "birthdate": "6 febrero 2000",
    "city": "city",
    "address": "tv 1d norte",
    "cellphone": "123",
    "image": null,
    "status": false,
    "__v": 0
}

但是我明白了

{
    "services": [
        {
            "worker_info": [],
            "_id": "5ce485a3288efe1834d4d71c",
            "user_address": "dbarva252",
            "description_service": "descripcion servicio",
            "skill_type": "wasting time",
            "amount_money": 0
        },
        {
            "worker_info": {
                "_id": "5ce4867e288efe1834d4d71e",
                "name": "Trabajador",
                "surname": "arreglador",
                "identification": "123457669",
                "birthdate": "1 febrero 1999",
                "skills": "time waster fixer",
                "image": null,
                "__v": 0
            }
        }
    ],
    "_id": "5ce48569288efe1834d4d71a",
    "user": "usuario123",
    "name": "mauricio",
    "surname": "barva",
    "identification": "123123",
    "birthdate": "6 febrero 2000",
    "city": "city",
    "address": "tv 1d norte",
    "cellphone": "123",
    "image": null,
    "status": false,
    "__v": 0
}

如您所见,它将替换所有信息。

0 个答案:

没有答案