1

I have the following structure:

"roles": [
  {
    "aspirants": ["Will Smith"],
    "description": {
      "content": "Role 1 content",
      "title": "Role 1"
    },
    "quantity": 12,
    "remunaration": 950
  },
  {
    "aspirants": ["Bruce Wayne"],
    "description": {
      "content": "Role 2 content",
      "title": "Role 2"
    },
    "quantity": 6,
    "remunaration": 1000
  }
]

So, what I need to do is to add a new aspirant matching the role he is postulating (that data is coming from the UI). For example, if "Tony Stark" is postulating to the "Role 1", then I have to update that field, so it will end as follows:

"roles": [
  {
    "aspirants": ["Will Smith", "Tony Stark"],
    "description": {
      "content": "Role 1 content",
      "title": "Role 1"
    },
    "quantity": 12,
    "remunaration": 950
  },
  {
    "aspirants": ["Bruce Wayne"],
    "description": {
      "content": "Role 2 content",
      "title": "Role 2"
    },
    "quantity": 6,
    "remunaration": 1000
  }
]

Any idea how can I achieve this? Of course, the roles are a field from a document.

1

1 Answer 1

1

You can use with arrayFilters

db.collection.update(
    {},
    {$addToSet:{'roles.$[r].aspirants':"Spider man"}},
    { arrayFilters: [{ 'r.description.title': 'Role 1' }], new: true }
)
Sign up to request clarification or add additional context in comments.

Comments

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.