1

I am developing a resume database Web Application using Node.js and MongoDB. I am trying to update a single record located inside a multidimensional array. How do I update a single array from the career_development field bellow given that each array have their own object ID?

{
    "_id": ObjectId("57f35a25983d521a90518efd"),
    "email": "[email protected]",
    "first_name": "John",
    "last_name": "Doe",
    "created_at": ISODate("2016-10-04T07:28:37.407Z"),
    "career_development": [
        {
            "_id": ObjectId("5811aefcb7880316e4497406"),
            "company_name": "TestCompanyName 1",
            "company_location": "Texas, United States"
        },
        {
            "_id": ObjectId("5811afb7b7880316e4497407"),
            "company_name": "TestCompanyName 2",
            "company_location": "South Carolina, United States"
        },
        {
            "_id": ObjectId("5811afbfb7880316e4497408"),
            "company_name": "TestCompanyName 3",
            "company_location": "Florida, United States"
        }
    ]
}
4
  • Use the $ positional operator with the dot notation as db.collection.update({ "career_development._id": ObjectId("5811aefcb7880316e4497406") }, { "$set": { "career_development.$.company_name": "TestCompanyName 1 Updated" } }) Commented Oct 28, 2016 at 11:36
  • @chridam thanks, that works. but what if say career_development is dynamic? how can I do it something like: var dynamic_variable = posted_data.type; { "$set": { dynamic_variable + ".$.company_name": "TestCompanyName 1 Updated" } } ?? Commented Oct 28, 2016 at 13:53
  • No problem. You can create an update object using the bracket notation as var dynamic_variable = posted_data.type; var query = {}; var updateObj = { "$set": { } }; query[dynamic_variable +"._id"] = ObjectId("5811aefcb7880316e4497406"); updateObj["$set"][dynamic_variable +".$.company_name"] = "TestCompanyName 1 Updated"; db.collection.update(query, updateObj) Commented Oct 28, 2016 at 14:08
  • 1
    @chridam Thanks for the help, it is working perfectly now. Could you please put both your 2 comments in 1 answer so I could mark it as the correct answer? Commented Oct 28, 2016 at 14:37

2 Answers 2

1

Use the $ positional operator with the dot notation as

db.collection.update(
    { "career_development._id": ObjectId("5811aefcb7880316e4497406") }, 
    { 
        "$set": { 
            "career_development.$.company_name": "TestCompanyName 1 Updated" 
        } 
    }
);

If career_development is dynamic, then you can create an update object using the bracket notation as:

var dynamic_variable = posted_data.type,
    query = {},
    updateObj = { "$set": { } }; 

query[dynamic_variable +"._id"] = ObjectId("5811aefcb7880316e4497406"); 
updateObj["$set"][dynamic_variable +".$.company_name"] = "TestCompanyName 1 Updated"; 
db.collection.update(query, updateObj)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this @chridam
@Carlo No worries, always happy to help :)
0

Are you saying that the array looks like this? If that is not the case we can user elemMatch to get the doc, loop over it and update it. Let me know, I will post the query over here.

"career_development": [
        {
            "_id": ObjectId("5811aefcb7880316e4497406"),
            "company_name": "TestCompanyName 1",
            "company_location": "Texas, United States"
        },
        {
            "_id": ObjectId("5811afb7b7880316e4497407"),
            "company_name": "TestCompanyName 2",
            "company_location": "South Carolina, United States"
        },
        {
            "_id": ObjectId("5811afbfb7880316e4497408"),
            "company_name": "TestCompanyName 3",
            "company_location": "Florida, United States"
        }
    ],
[
        {
            "_id": ObjectId("5811aefcb7880316e4497406"),
            "company_name": "TestCompanyName 1",
            "company_location": "Texas, United States"
        },
        {
            "_id": ObjectId("5811afb7b7880316e4497407"),
            "company_name": "TestCompanyName 2",
            "company_location": "South Carolina, United States"
        },
        {
            "_id": ObjectId("5811afbfb7880316e4497408"),
            "company_name": "TestCompanyName 3",
            "company_location": "Florida, United States"
        }
    ]

1 Comment

Not sure what you did there, but I need to update (by object ID) a single record / array inside the career_development field which contains multiple arrays. Can you give me an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.