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
14971821
Commit
14971821
authored
May 03, 2015
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-94: BulkWriteResult and collect inserted IDs in bulkWrite()
parent
bfd68613
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
2 deletions
+149
-2
BulkWriteResult.php
src/BulkWriteResult.php
+136
-0
Collection.php
src/Collection.php
+13
-2
No files found.
src/BulkWriteResult.php
0 → 100644
View file @
14971821
<?php
namespace
MongoDB
;
use
BSON\ObjectId
;
use
MongoDB\Driver\WriteResult
;
/**
* Result class for a bulk write operation.
*/
class
BulkWriteResult
{
private
$writeResult
;
private
$insertedIds
;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
public
function
__construct
(
WriteResult
$writeResult
,
array
$insertedIds
)
{
$this
->
writeResult
=
$writeResult
;
$this
->
insertedIds
=
$insertedIds
;
}
/**
* Return the number of documents that were deleted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public
function
getDeletedCount
()
{
return
$this
->
writeResult
->
getDeletedCount
();
}
/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public
function
getInsertedCount
()
{
return
$this
->
writeResult
->
getInsertedCount
();
}
/**
* Return a map of the inserted documents' IDs.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If the document had an ID prior to insertion (i.e. the
* driver did not generate an ID), this will contain its "_id" field value.
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
*
* @return mixed[]
*/
public
function
getInsertedIds
()
{
return
$this
->
insertedIds
;
}
/**
* Return the number of documents that were matched by the filter.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public
function
getMatchedCount
()
{
return
$this
->
writeResult
->
getMatchedCount
();
}
/**
* Return the number of documents that were modified.
*
* This value is undefined if the write was not acknowledged or if the write
* executed as a legacy operation instead of write command.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer|null
*/
public
function
getModifiedCount
()
{
return
$this
->
writeResult
->
getModifiedCount
();
}
/**
* Return the number of documents that were upserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
* @return integer
*/
public
function
getUpsertedCount
()
{
return
$this
->
writeResult
->
getUpsertedCount
();
}
/**
* Return a map of the upserted documents' IDs.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If the document had an ID prior to upserting (i.e. the
* server did not need to generate an ID), this will contain its "_id". Any
* server-generated ID will be an MongoDB\Driver\ObjectID instance.
*
* @return mixed[]
*/
public
function
getUpsertedIds
()
{
return
$this
->
writeResult
->
getUpsertedIds
();
}
/**
* Return whether this update was acknowledged by the server.
*
* If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined.
*
* @return boolean
*/
public
function
isAcknowledged
()
{
return
$this
->
writeResult
->
isAcknowledged
();
}
}
src/Collection.php
View file @
14971821
...
@@ -207,6 +207,7 @@ class Collection
...
@@ -207,6 +207,7 @@ class Collection
$options
=
array_merge
(
$this
->
getBulkOptions
(),
$options
);
$options
=
array_merge
(
$this
->
getBulkOptions
(),
$options
);
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$bulk
=
new
BulkWrite
(
$options
[
"ordered"
]);
$insertedIds
=
array
();
foreach
(
$ops
as
$n
=>
$op
)
{
foreach
(
$ops
as
$n
=>
$op
)
{
foreach
(
$op
as
$opname
=>
$args
)
{
foreach
(
$op
as
$opname
=>
$args
)
{
...
@@ -216,7 +217,14 @@ class Collection
...
@@ -216,7 +217,14 @@ class Collection
switch
(
$opname
)
{
switch
(
$opname
)
{
case
"insertOne"
:
case
"insertOne"
:
$bulk
->
insert
(
$args
[
0
]);
$insertedId
=
$bulk
->
insert
(
$args
[
0
]);
if
(
$insertedId
!==
null
)
{
$insertedIds
[
$n
]
=
$insertedId
;
}
else
{
$insertedIds
[
$n
]
=
is_array
(
$args
[
0
])
?
$args
[
0
][
'_id'
]
:
$args
[
0
]
->
_id
;
}
break
;
break
;
case
"updateMany"
:
case
"updateMany"
:
...
@@ -269,7 +277,10 @@ class Collection
...
@@ -269,7 +277,10 @@ class Collection
}
}
}
}
}
}
return
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
$writeResult
=
$this
->
manager
->
executeBulkWrite
(
$this
->
ns
,
$bulk
,
$this
->
wc
);
return
new
BulkWriteResult
(
$writeResult
,
$insertedIds
);
}
}
/**
/**
...
...
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