Dfranc3373
Dfranc3373

Reputation: 2177

Updating a nested subdocument's array - Mongoose

Currently I have the following structure for one of my documents

Company: {
   Buildings: [{
       Users: [ { _id: ObjectID, name: String, number: String } ]
   }]
}

I'm trying to update the user's name and number and currently have tested and verified the following query in mongo:

db.companies.update(
    { "_id": companyID, "buildings._id": buildingID, "buildings.users._id": userID }
    , 
    { $set: { "buildings.$.users.0.name": "A new name for the user" } }
);

This query updates correctly however when I run the same exact query using mongoose

Company.findOneAndUpdate(
  { _id: companyID, "buildings._id": buildingID, "buildings.users._id": userID }
  ,
  { $set: { "buildings.$.users.0.name": newName }})

I get no error but the update is not performed.

Is the updating of a deep nested array not available on Mongoose?

Upvotes: 1

Views: 7567

Answers (1)

Dfranc3373
Dfranc3373

Reputation: 2177

Answer was found in an alternative answer to this question:

Answer: https://stackoverflow.com/a/28952991/1327815

Upvotes: 1

Related Questions