0

I want to add 2 arrays in phone array to the Mongo database.

$addToSet and $each show how to add 1n array with objects to another array, but I want to add 2 or more arrays not objects.

To add one array in phone, I used the command:

$col->update(array("email"=>$email), array('$addToSet'=>array("phone" =>  array("id" => "3","no" => "+1 000 0000","name" => "Bob"))));

Example document

"_id": "id",
"email": "Name@google.com",
"phone": [
  {
    "id": "1",
    "name": "Bob",
    "no": "+1 000 0000",
  },
  {
    "id": "2",
    "name": "Bob",
    "no": "+1 000 0000",

  }
]

I tried to add 2 arrays but it did not work

$col->update(array("email"=>$email), array('$addToSet'=>array("phone" => array(("id" => "6","name" => "Bob","no" => "+1 000 0000"),("id" => "7","name" => "Bob","no" => "+1 000 0000"))))); 

I have a syntax error here when I try to add multiple arrays with objects:

array(("id" => "6","name" => "Bob","no" => "+1 000 0000"),("id" => "7","name" => "Bob","no" => "+1 000 0000"))

How can I remove this syntax error when adding 2 arrays to the database?

Vitalii
  • 47
  • 6
  • [`$each`](https://docs.mongodb.com/manual/reference/operator/update/each/) is a modifier applicable to `$push` and `$addToSet` that allows you to specify and array of objects to add at once. Note this is not the "accepted" answer on the linked question. – Neil Lunn May 17 '18 at 09:52
  • $ each in the example adds several parameters and not several arrays – Vitalii May 17 '18 at 10:02
  • this is not a duplicate. I'm trying to add not 1n array with objects but a few, these are different things! – Vitalii May 17 '18 at 10:15

0 Answers0