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
265643d8
Commit
265643d8
authored
Dec 09, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHP-1312: Implement Collection::bulkWrite()
parent
b9953f42
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
133 additions
and
7 deletions
+133
-7
write.php
examples/write.php
+55
-1
Collection.php
src/MongoDB/Collection.php
+78
-6
No files found.
examples/write.php
View file @
265643d8
...
...
@@ -155,6 +155,60 @@ try {
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
exit
;
}
try
{
$result
=
$collection
->
bulkWrite
(
// Required writes param (an array of operations)
[
// Like explain(), operations identified by single key
[
'insertOne'
=>
[
[
'x'
=>
1
]
],
],
[
'updateMany'
=>
[
[
'x'
=>
1
],
[
'$set'
=>
[
'x'
=>
2
]],
],
],
[
'updateOne'
=>
[
[
'x'
=>
3
],
[
'$set'
=>
[
'x'
=>
4
]],
// Optional params are still permitted
[
'upsert'
=>
true
],
],
],
[
'deleteOne'
=>
[
[
'x'
=>
1
],
],
],
[
'deleteMany'
=>
[
// Required arguments must still be specified
[],
],
],
],
// Optional named params in an associative array
[
'ordered'
=>
false
]
);
printf
(
"insertedCount: %d
\n
"
,
$result
->
getInsertedCount
());
printf
(
"matchedCount: %d
\n
"
,
$result
->
getMatchedCount
());
printf
(
"modifiedCount: %d
\n
"
,
$result
->
getModifiedCount
());
printf
(
"upsertedCount: %d
\n
"
,
$result
->
getUpsertedCount
());
printf
(
"deletedCount: %d
\n
"
,
$result
->
getDeletedCount
());
foreach
(
$result
->
getUpsertedIds
()
as
$index
=>
$id
)
{
printf
(
"upsertedId[%d]: %s"
,
$index
,
$id
);
}
}
catch
(
Exception
$e
)
{
printf
(
"Caught exception '%s', on line %d
\n
"
,
$e
->
getMessage
(),
__LINE__
);
echo
$e
->
getTraceAsString
(),
"
\n
"
;
exit
;
}
src/MongoDB/Collection.php
View file @
265643d8
...
...
@@ -179,6 +179,78 @@ class Collection {
"limit"
=>
1
,
);
}
/* }}} */
function
getBulkOptions
()
{
/* {{{ */
return
array
(
"ordered"
=>
false
,
);
}
/* }}} */
function
bulkWrite
(
array
$bulk
,
array
$options
=
array
())
{
$options
=
array_merge
(
$this
->
getBulkOptions
(),
$options
);
$batch
=
new
WriteBatch
(
$options
[
"ordered"
]);
foreach
(
$bulk
as
$n
=>
$op
)
{
foreach
(
$op
as
$opname
=>
$args
)
{
if
(
!
isset
(
$args
[
0
]))
{
throw
\RuntimeException
(
sprintf
(
"Missing argument#1 for '%s' (operation#%d)"
,
$opname
,
$n
));
}
switch
(
$opname
)
{
case
"insertOne"
:
$batch
->
insert
(
$args
[
0
]);
break
;
case
"updateMany"
:
if
(
!
isset
(
$args
[
1
]))
{
throw
\RuntimeException
(
sprintf
(
"Missing argument#2 for '%s' (operation#%d)"
,
$opname
,
$n
));
}
$options
=
array_merge
(
$this
->
getWriteOptions
(),
isset
(
$args
[
2
])
?
$args
[
2
]
:
array
(),
array
(
"limit"
=>
0
));
$batch
->
update
(
$args
[
0
],
$args
[
1
],
$options
);
break
;
case
"updateOne"
:
if
(
!
isset
(
$args
[
1
]))
{
throw
\RuntimeException
(
sprintf
(
"Missing argument#2 for '%s' (operation#%d)"
,
$opname
,
$n
));
}
$options
=
array_merge
(
$this
->
getWriteOptions
(),
isset
(
$args
[
2
])
?
$args
[
2
]
:
array
(),
array
(
"limit"
=>
1
));
if
(
key
(
$args
[
1
])[
0
]
!=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must be a
\$
operator"
);
}
$batch
->
update
(
$args
[
0
],
$args
[
1
],
$options
);
break
;
case
"replaceOne"
:
if
(
!
isset
(
$args
[
1
]))
{
throw
\RuntimeException
(
sprintf
(
"Missing argument#2 for '%s' (operation#%d)"
,
$opname
,
$n
));
}
$options
=
array_merge
(
$this
->
getWriteOptions
(),
isset
(
$args
[
2
])
?
$args
[
2
]
:
array
(),
array
(
"limit"
=>
1
));
if
(
key
(
$args
[
1
])[
0
]
==
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must NOT be a
\$
operator"
);
}
$batch
->
update
(
$args
[
0
],
$args
[
1
],
$options
);
break
;
case
"deleteOne"
:
$options
=
array_merge
(
$this
->
getWriteOptions
(),
isset
(
$args
[
1
])
?
$args
[
1
]
:
array
(),
array
(
"limit"
=>
1
));
$batch
->
delete
(
$args
[
0
],
$options
);
break
;
case
"deleteMany"
:
$options
=
array_merge
(
$this
->
getWriteOptions
(),
isset
(
$args
[
1
])
?
$args
[
1
]
:
array
(),
array
(
"limit"
=>
1
));
$batch
->
delete
(
$args
[
0
],
$options
);
break
;
default
:
throw
\RuntimeException
(
sprintf
(
"Unknown operation type called '%s' (operation#%d)"
,
$opname
,
$n
));
}
}
}
return
$this
->
manager
->
executeWriteBatch
(
$this
->
ns
,
$batch
,
$this
->
wc
);
}
function
insertOne
(
array
$filter
)
{
/* {{{ */
$options
=
array_merge
(
$this
->
getWriteOptions
());
...
...
@@ -215,17 +287,17 @@ class Collection {
$batch
->
update
(
$filter
,
$update
,
$options
);
return
$this
->
manager
->
executeWriteBatch
(
$this
->
ns
,
$batch
,
$this
->
wc
);
}
/* }}} */
function
updat
eOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
/* {{{ */
if
(
key
(
$update
)[
0
]
!
=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must be a
\$
operator"
);
function
replac
eOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
/* {{{ */
if
(
key
(
$update
)[
0
]
=
=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must
NOT
be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
return
new
UpdateResult
(
$wr
);
}
/* }}} */
function
replac
eOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
/* {{{ */
if
(
key
(
$update
)[
0
]
=
=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must
NOT
be a
\$
operator"
);
function
updat
eOne
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
/* {{{ */
if
(
key
(
$update
)[
0
]
!
=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must be a
\$
operator"
);
}
$wr
=
$this
->
_update
(
$filter
,
$update
,
$options
);
...
...
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