I have a structure like this:
_id: ObjectId("3c4b1475238d3b4dd5000001"),
username: "kris",
addresses:
[
{
name: "home",
street: "123 Some Ave",
city: "Cul City",
state: "CA",
zip: 12345
},
{
name: "work",
street: "1234 Sea Blvd",
city: "Cul City",
state: "CA",
zip: 54321
}
]
I'm trying to do this:
UID=ObjectId("3c4b1475238d3b4dd5000001")
doc=db.users.findOne({_id: UID})
new_address={name:'work', street:'17w. 18th St', city: 'New York', state:'NY', zip:10011}
doc['addresses'].append(new_address)
But i get this error: uncaught exception: TypeError: doc.addresses.append is not a function : How can i solve it?
CodePudding user response:
The proper javascript
method to append to an array is push
.
So, instead of:
doc['addresses'].append(new_address)
use:
doc['addresses'].push(new_address)