0

I am new at MongoDB and I want to append an "address 2" to the "location" array of my "CUSTOMER" document that has first name of "Jimmy".

The collection is like this:

db.collection.insert(
  {
    "CUSTOMER": {
      "first name": "Jimmy",
      "location": [{"address": {"num": 1}}]
    }
  }
); 

I tried the following, but it created a new set insted putting a new address inside the array of location:

db.collection.update({"CUSTOMER.first name": "Jimmy"}, {$set:{"location":{"address": "num":2 }}}); 

I tried to put the "location" prior to the $set put it doesn't work.

I need the right prototype of doing such thing.

The expected outcome is the following:

db.collection.insert(
  {
    "CUSTOMER": {
      "first name": "Jimmy",
      "location": [{{"address": "num": 1}},
                   {"address": "num": 2}}
]
    }
  }
); 

To sum up, I want to put a new address to the array of location above.

2
  • can you post expected output? Commented Jun 4, 2020 at 6:06
  • { "CUSTOMER": { "first name": "Jimmy", "location": [{{"address": {"num": 1}}, {"address": {"num": 2}] } } Commented Jun 4, 2020 at 6:16

1 Answer 1

1

$set won't help here you'll have to use $push operator

db.customers.update({"CUSTOMER.first name" : "Jimmy"}, {$push: {"CUSTOMER.location": {"address" : {"num" : 1}}}})
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.