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"
}
]
}
$positional operator with the dot notation asdb.collection.update({ "career_development._id": ObjectId("5811aefcb7880316e4497406") }, { "$set": { "career_development.$.company_name": "TestCompanyName 1 Updated" } })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)