Reputation: 33
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.
Upvotes: 0
Views: 89
Reputation: 1086
$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}}}})
Upvotes: 1