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
865685e1
Commit
865685e1
authored
Dec 05, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHP-1302: Collection::count()
parent
d337c985
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
22 deletions
+74
-22
write.php
examples/write.php
+10
-21
Collection.php
src/MongoDB/Collection.php
+64
-1
No files found.
examples/write.php
View file @
865685e1
...
...
@@ -33,14 +33,9 @@ try {
$result
=
$collection
->
insertOne
(
$bobby
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
find
(
array
(
"nick"
=>
"bjori"
),
array
(
"projection"
=>
array
(
"name"
=>
1
)));
echo
"Searching for nick => bjori, should have only one result:
\n
"
;
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$count
=
$collection
->
count
(
array
(
"nick"
=>
"bjori"
));
printf
(
"Searching for nick => bjori, should have only one result: %d
\n
"
,
$count
);
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %s (out of expected 1)
\n
"
,
$result
->
getNumRemoved
());
$result
=
$collection
->
updateOne
(
array
(
"citizen"
=>
"USA"
),
array
(
'$set'
=>
array
(
"citizen"
=>
"Iceland"
))
...
...
@@ -52,33 +47,24 @@ try {
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %d (out of expected 1)
\n
"
,
$result
->
getNumRemoved
());
}
catch
(
Exception
$e
)
{
echo
$e
->
getMessage
(),
"
\n
"
;
exit
;
}
try
{
/* These two were removed earlier */
$result
=
$collection
->
insertOne
(
$hannes
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
insertOne
(
$hayley
);
printf
(
"Inserted: %s (out of expected 1)
\n
"
,
$result
->
getNumInserted
());
$result
=
$collection
->
find
();
echo
"Find all docs, should be 3, verify
2x USA citizen, 1
Icelandic
\n
"
;
echo
"Find all docs, should be 3, verify
1x USA citizen, 2x
Icelandic
\n
"
;
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
$result
=
$collection
->
updateMany
(
array
(
"citizen"
=>
"
USA
"
),
array
(
'$set'
=>
array
(
"
citizen"
=>
"Iceland"
))
array
(
"citizen"
=>
"
Iceland
"
),
array
(
'$set'
=>
array
(
"
viking"
=>
true
))
);
printf
(
"Updated: %d (out of expected 2), verify
everyone is Icelandic
\n
"
,
$result
->
getNumModified
());
printf
(
"Updated: %d (out of expected 2), verify
Icelandic people are vikings
\n
"
,
$result
->
getNumModified
());
$result
=
$collection
->
find
();
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
...
...
@@ -93,8 +79,11 @@ try {
var_dump
(
$document
);
}
$result
=
$collection
->
deleteOne
(
$document
);
printf
(
"Deleted: %d (out of expected 1)
\n
"
,
$result
->
getNumRemoved
());
$result
=
$collection
->
deleteMany
(
array
(
"citizen"
=>
"Iceland"
));
printf
(
"Deleted: %d (out of expected
3
)
\n
"
,
$result
->
getNumRemoved
());
printf
(
"Deleted: %d (out of expected
2
)
\n
"
,
$result
->
getNumRemoved
());
}
catch
(
Exception
$e
)
{
echo
$e
->
getMessage
(),
"
\n
"
;
exit
;
...
...
src/MongoDB/Collection.php
View file @
865685e1
...
...
@@ -4,6 +4,7 @@ namespace MongoDB;
/* phongo includes */
use
MongoDB\Manager
;
use
MongoDB\Query
;
use
MongoDB\Command
;
use
MongoDB\ReadPreference
;
use
MongoDB\WriteBatch
;
...
...
@@ -20,11 +21,14 @@ class Collection {
protected
$wc
;
protected
$ns
;
protected
$dbname
;
protected
$collname
;
function
__construct
(
Manager
$manager
,
$ns
,
WriteConcern
$wc
=
null
,
ReadPreference
$rp
=
null
)
{
$this
->
manager
=
$manager
;
$this
->
ns
=
$ns
;
$this
->
wc
=
$wc
;
$this
->
rp
=
$rp
;
list
(
$this
->
dbname
,
$this
->
collname
)
=
explode
(
"."
,
$ns
,
2
);
}
function
find
(
array
$filter
=
array
(),
array
$options
=
array
())
{
/* {{{ {{{ */
...
...
@@ -159,7 +163,7 @@ class Collection {
return
$query
;
}
/* }}} */
/* }}} */
/* {{{ writes */
protected
function
_writeSingle
(
$filter
,
$type
,
array
$options
=
array
(),
$newobj
=
array
())
{
/* {{{ */
$options
=
array_merge
(
$this
->
getWriteOptions
(),
$options
);
...
...
@@ -212,5 +216,64 @@ class Collection {
function
updateMany
(
array
$filter
,
$update
,
array
$options
=
array
())
{
/* {{{ */
return
$this
->
_writeSingle
(
$filter
,
self
::
UPDATE
,
$options
+
array
(
"limit"
=>
0
),
$update
);
}
/* }}} */
/* }}} */
function
count
(
array
$filter
=
array
(),
array
$options
=
array
())
{
/* {{{ */
$options
=
array_merge
(
$this
->
getFindOptions
(),
$options
);
$cmd
=
array
(
"count"
=>
$this
->
collname
,
"query"
=>
$filter
,
$options
);
$doc
=
$this
->
_runCommand
(
$this
->
dbname
,
$cmd
)
->
getResponseDocument
();
if
(
$doc
[
"ok"
])
{
return
$doc
[
"n"
];
}
throw
$this
->
_generateCommandException
(
$doc
);
}
/* }}} */
function
getCountOptions
()
{
/* {{{ */
return
array
(
/**
* The index to use.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"hint"
=>
""
,
// string or document
/**
* The maximum number of documents to count.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"limit"
=>
0
,
/**
* The maximum amount of time to allow the query to run.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"maxTimeMS"
=>
0
,
/**
* The number of documents to skip before returning the documents.
*
* @see http://docs.mongodb.org/manual/reference/command/count/
*/
"skip"
=>
0
,
);
}
/* }}} */
protected
function
_generateCommandException
(
$doc
)
{
/* {{{ */
if
(
$doc
[
"errmsg"
])
{
return
new
Exception
(
$doc
[
"errmsg"
]);
}
var_dump
(
$doc
);
return
new
Exception
(
"FIXME: Unknown error"
);
}
/* }}} */
protected
function
_runCommand
(
$dbname
,
array
$cmd
,
ReadPreference
$rp
=
null
)
{
/* {{{ */
$command
=
new
Command
(
$cmd
);
return
$this
->
manager
->
executeCommand
(
$dbname
,
$command
,
$rp
);
}
/* }}} */
}
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