1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
=================================
MongoDB\\Collection::insertMany()
=================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\Collection::insertMany
Inserts one document.
.. code-block:: php
function insertMany(array $documents, array $options = []): MongoDB\InsertManyResult
``insertMany()`` supports the following parameters:
.. include:: /includes/apiargs/MongoDBCollection-method-insertMany-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBCollection-method-insertMany-option.rst
Output
------
Returns a ``MongoDB\InsertManyResult`` object.
Example
-------
.. start-crud-include
The following operation inserts two documents into the ``users``
collection in the ``example`` database:
.. code-block:: php
<?php
$database = new MongoDB\Client;
$collection = $database->selectCollection('users','restaurants');
$newUsers = $collection->insertMany(
[
[
'username' => 'admin',
'email' => 'admin@example.com',
'name' => 'Admin User'
],
[
'username' => 'test',
'email' => 'test@example.com',
'name' => 'Test User'
],
]
);
printf("Inserted %d document(s)\n", $newUsers->getInsertedCount());
var_dump($newUsers->getInsertedIds());
The output would resemble::
Inserted 2 document(s)
array(2) {
[0]=>
object(MongoDB\BSON\ObjectID)#11 (1) {
["oid"]=>
string(24) "579a25921f417dd1e5518141"
}
[1]=>
object(MongoDB\BSON\ObjectID)#12 (1) {
["oid"]=>
string(24) "579a25921f417dd1e5518142"
}
}
.. end-crud-include
.. seealso::
- :phpmethod:`MongoDB\\Collection::bulkWrite`
- :phpmethod:`MongoDB\\Collection::insertOne`
- :doc:`/tutorial/crud`
- :manual:`insert </reference/command/insert>` command reference
in the MongoDB manual