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
124f8ad4
Commit
124f8ad4
authored
Dec 08, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHP-1315: Implement Collection::findOneAndUpdate()
parent
d92248a6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
0 deletions
+65
-0
write.php
examples/write.php
+4
-0
Collection.php
src/MongoDB/Collection.php
+61
-0
No files found.
examples/write.php
View file @
124f8ad4
...
...
@@ -136,6 +136,10 @@ try {
echo
"Kasparov
\n
"
;
var_dump
(
$result
);
echo
"Returning the old document where he was Russian
\n
"
;
$result
=
$collection
->
findOneAndUpdate
(
$kasparov
,
array
(
'$set'
=>
array
(
"citizen"
=>
"Croatia"
)));
var_dump
(
$result
);
echo
"Deleting him, he isn't Croatian just yet
\n
"
;
$result
=
$collection
->
findOneAndDelete
(
array
(
"citizen"
=>
"Croatia"
));
var_dump
(
$result
);
...
...
src/MongoDB/Collection.php
View file @
124f8ad4
...
...
@@ -472,6 +472,67 @@ class Collection {
}
/* }}} */
function
findOneAndUpdate
(
array
$filter
,
array
$update
,
array
$options
=
array
())
{
/* {{{ */
if
(
key
(
$update
)[
0
]
!=
'$'
)
{
throw
new
\RuntimeException
(
"First key in
\$
update must be a
\$
operator"
);
}
$options
=
array_merge
(
$this
->
getFindOneAndUpdateOptions
(),
$options
);
$options
=
$this
->
_massageFindAndModifyOptions
(
$options
,
$update
);
$cmd
=
array
(
"findandmodify"
=>
$this
->
collname
,
"query"
=>
$filter
,
)
+
$options
;
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
getResponseDocument
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"value"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
/* }}} */
function
getFindOneAndUpdateOptions
()
{
/* {{{ */
return
array
(
/**
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"maxTimeMS"
=>
0
,
/**
* Limits the fields to return for all matching documents.
*
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results
*/
"projection"
=>
array
(),
/**
* When ReturnDocument.After, returns the updated or inserted document rather than the original.
* Defaults to ReturnDocument.Before.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"returnDocument"
=>
self
::
FIND_ONE_AND_RETURN_BEFORE
,
/**
* Determines which document the operation modifies if the query selects multiple documents.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"sort"
=>
array
(),
/**
* When true, creates a new document if no document matches the query. The default is false.
*
* @see http://docs.mongodb.org/manual/reference/command/findAndModify/
*/
"upsert"
=>
false
,
);
}
/* }}} */
protected
function
_massageFindAndModifyOptions
(
$options
,
$update
=
array
())
{
/* {{{ */
$ret
=
array
(
...
...
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