0

I've this simple json:

{
   id: 1,
   basket: {
        box: []
   }
}

I need to add itens inside the box array that's child of the basket array.

My attempt:

updateOne({ id: 1},  {
                "$push": {
                    "basket": {
                        "box": {
                            "dummy": "test"
                        }
                    }
                }
            }

but it's not working. I am receiving this exception:

UnhandledPromiseRejectionWarning: MongoError: The field 'basket' must be an array but is of type object in document

1 Answer 1

2

You can access your object like so :

findOne({ id: 1}, 
{
  "$push": {
      "basket.box": {
        "dummy": "test"
      }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do or do not, there is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
No lucky...now I got a UnhandledPromiseRejectionWarning: MongoError: The field 'basket.bopx' must be an array but is of type object in document
Can you place your schema in your question please ?
@MarcoJr Please check if the error you are getting because you have tried wrong way in your own solution that made the basket.box as object. Please delete the exiting records and try again, It should work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.