Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
af72fa5f
Commit
af72fa5f
authored
Mar 17, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-59: Implement insertMany() and InsertManyResult
parent
55cf25d0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
0 deletions
+94
-0
Collection.php
src/Collection.php
+28
-0
InsertManyResult.php
src/InsertManyResult.php
+66
-0
No files found.
src/Collection.php
View file @
af72fa5f
...
@@ -422,6 +422,34 @@ class Collection
...
@@ -422,6 +422,34 @@ class Collection
return
new
InsertOneResult
(
$wr
,
$id
);
return
new
InsertOneResult
(
$wr
,
$id
);
}
}
/**
* Inserts the provided documents
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $documents The documents to insert
* @return InsertManyResult
*/
public
function
insertMany
(
array
$documents
)
{
$options
=
array_merge
(
$this
->
getWriteOptions
());
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$insertedIds
=
array
();
foreach
(
$documents
as
$i
=>
$document
)
{
$insertedId
=
$bulk
->
insert
(
$document
);
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$i
]
=
$insertedId
;
}
}
$writeResult
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
InsertManyResult
(
$writeResult
,
$insertedIds
);
}
/**
/**
* Internal helper for delete one/many documents
* Internal helper for delete one/many documents
* @internal
* @internal
...
...
src/InsertManyResult.php
0 → 100644
View file @
af72fa5f
<?php
namespace
MongoDB
;
use
MongoDB\Driver\WriteResult
;
/**
* Result class for a multi-document write operation.
*/
class
InsertManyResult
{
private
$writeResult
;
private
$insertedIds
;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param array $insertedIds
*/
public
function
__construct
(
WriteResult
$writeResult
,
array
$insertedIds
=
array
())
{
$this
->
writeResult
=
$writeResult
;
$this
->
insertedIds
=
$insertedIds
;
}
/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see InsertManyResult::isAcknowledged()
* @return integer
*/
public
function
getInsertedCount
()
{
return
$this
->
writeResult
->
getInsertedCount
();
}
/**
* Return the map of inserted IDs that were generated by the driver.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If an inserted document already had an ID (e.g. it was
* generated by the application), it will not be present in this map.
*
* @return array
*/
public
function
getInsertedIds
()
{
return
$this
->
insertedIds
;
}
/**
* Return whether this insert result was acknowledged by the server.
*
* If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined.
*
* @return boolean
*/
public
function
isAcknowledged
()
{
return
$this
->
writeResult
->
isAcknowledged
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment