V
V
Vitaly2018-05-17 14:40:28
PHP
Vitaly, 2018-05-17 14:40:28

How to add multiple arrays at once to a MongoDB document using PHP?

I want to add 2 arrays to the Mongo DB database at once.
In order to add 1 array to the phone array, I used the command:

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

Sample document
"_id": "id",
"email": "[email protected]",
"phone": [
  {
    "id": "1",
    "name": "Bob",
    "no": "+1 000 0000",
  },
  {
    "id": "2",
    "name": "Bob",
    "no": "+1 000 0000",
  }
]

I tried to add 2 arrays at once to the phone array
$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")))));

But I get a syntax error. There are plenty of examples on the Internet on how to add a 1n array with several values ​​using the $addToSet and $each operators, but there are no examples of how to add several arrays at once.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iljaGolubev, 2018-05-17
@TokyoZero

https://docs.mongodb.com/manual/reference/operator...
not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question