3

My mongoose object:

{
    "_id" : "568ad3db59b494d4284ac191",
    "name" : "MyCompany",
    "description": "whatever"
    "items" : [ 
        {
            "_id" : "568ad3db59b494d4284ac19f",
            "fields" : {
                "Name" : "Item1",
                "Internal ID" : "ID00042",
                "tags" : [ 
                    {
                        "Description" : "Tag1",
                        "Level" : 2
                    }, 
                    {
                        "Description" : "Tag2",
                        "Level" : 3
                    }
                ]
            }
        },         {
            "_id" : "568ad3db59b494d4284ac19f",
            "fields" : {
                "Name" : "Item2",
                "Internal ID" : "ID00043",
                "tags" : [ 
                    {
                        "Description" : "Tag1",
                        "Level" : 5
                    }, 
                    {
                        "Description" : "Tag5",
                        "Level" : 1
                    }
                ]
            }
        }, {..}
    ]
}

I need to push the following tag:

var obj = {
      "Description" : "myDescription",
      "Level" : 3
};

Into the following item's tag array:

var internal_id = "ID00102";

My attempt is not working:

Company.findOneAndUpdate(
    { "_id": "568ad3db59b494d4284ac191", "items.fields['Internal ID]": internal_id },
    {
        "$push": {
            "tags": thetag
        }
    },
    function(err,doc) {
        if (err) res.status(500).send(err);
        return res.status(200).send(doc);
    }
);

2 Answers 2

2

Apply the $push operator together with the $ positional operator in your update to add the tag object to the tags field. The $ positional operator will identify the correct element in the items array to update without explicitly specifying the position of the element in the array, thus your final update statement should look like:

Company.update(
    { "_id": "568ad3db59b494d4284ac191", "items.fields.Internal ID": internal_id },
    {
        "$push": {
            "items.$.fields.tags": thetag
        }
    }
)
Sign up to request clarification or add additional context in comments.

Comments

0
Company.findOneAndUpdate(
{ "_id": "568ad3db59b494d4284ac191", "items.fields['Internal ID]": internal_id },
    {
        "$push": {
            "tags": thetag
        }
    },
    function(err,doc) {
        if (err) res.status(500).send(err);
        return res.status(200).send(doc);
    }
);

In the example above:

{ "_id": "568ad3db59b494d4284ac191", "items.fields['Internal ID]": internal_id },

MISSING ' at "items.fields['Internal ID]"

Having spaces in object names isn't good practice as in "items.fields.Internal ID", it doesn't always work and gives some awkward issues. using internalId instead just makes things less awkward.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.