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
7d9e0990
Commit
7d9e0990
authored
Dec 04, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHP-1304: Implement Collection::find()
parents
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
190 additions
and
0 deletions
+190
-0
find.php
examples/find.php
+19
-0
Collection.php
src/Collection.php
+171
-0
No files found.
examples/find.php
0 → 100644
View file @
7d9e0990
<?php
require
__DIR__
.
"/../src/Collection.php"
;
$manager
=
new
MongoDB\Manager
(
"mongodb://localhost:27017"
);
var_dump
(
$manager
);
$collection
=
new
MongoDB\Collection
(
$manager
,
"phongo_test.functional_cursor_001"
);
$result
=
$collection
->
find
(
array
(
"username"
=>
"pacocha.quentin"
),
array
(
"projection"
=>
array
(
"firstName"
=>
1
)));
foreach
(
$result
as
$document
)
{
var_dump
(
$document
);
}
src/Collection.php
0 → 100644
View file @
7d9e0990
<?php
namespace
MongoDB
;
use
MongoDB\Manager
;
use
MongoDB\Query
;
use
MongoDB\ReadPreference
;
class
QueryFlags
{
const
TAILABLE_CURSOR
=
0x02
;
const
SLAVE_OKAY
=
0x04
;
const
OPLOG_REPLY
=
0x08
;
const
NO_CURSOR_TIMEOUT
=
0x10
;
const
AWAIT_DATA
=
0x20
;
const
EXHAUST
=
0x40
;
const
PARTIAL
=
0x80
;
}
class
CursorType
{
const
NON_TAILABLE
=
0x00
;
const
TAILABLE
=
QueryFlags
::
TAILABLE_CURSOR
;
/* QueryFlags::TAILABLE_CURSOR | QueryFlags::AWAIT_DATA; */
const
TAILABLE_AWAIT
=
0x22
;
}
class
Collection
{
protected
$manager
;
protected
$rp
;
protected
$ns
;
function
__construct
(
Manager
$manager
,
$ns
,
ReadPreference
$rp
=
null
)
{
$this
->
manager
=
$manager
;
$this
->
ns
=
$ns
;
$this
->
rp
=
$rp
;
}
protected
function
_opQueryFlags
(
$options
)
{
$flags
=
0
;
$flags
|=
$options
[
"allowPartialResults"
]
?
QueryFlags
::
PARTIAL
:
0
;
$flags
|=
$options
[
"cursorType"
]
?
$options
[
"cursorType"
]
:
0
;
$flags
|=
$options
[
"oplogReplay"
]
?
QueryFlags
::
OPLOG_REPLY
:
0
;
$flags
|=
$options
[
"noCursorTimeout"
]
?
QueryFlags
::
NO_CURSOR_TIMEOUT
:
0
;
return
$flags
;
}
protected
function
_buildQuery
(
$document
,
$options
)
{
if
(
$options
[
"comment"
])
{
$options
[
"modifiers"
][
'$comment'
]
=
$options
[
"comment"
];
}
if
(
$options
[
"maxTimeMS"
])
{
$options
[
"modifiers"
][
'$maxTimeMS'
]
=
$options
[
"maxTimeMS"
];
}
if
(
$options
[
"sort"
])
{
$options
[
'$orderby'
]
=
$options
[
"sort"
];
}
$flags
=
$this
->
_opQueryFlags
(
$options
);
$options
[
"cursorFlags"
]
=
$flags
;
$query
=
new
Query
(
$document
,
$options
);
return
$query
;
}
function
find
(
array
$document
=
array
(),
array
$options
=
array
())
{
$options
=
array_merge
(
$this
->
getFindOptions
(),
$options
);
$query
=
$this
->
_buildQuery
(
$document
,
$options
);
$cursor
=
$this
->
manager
->
executeQuery
(
$this
->
ns
,
$query
,
$this
->
rp
);
return
$cursor
;
}
function
getFindOptions
()
{
return
array
(
/**
* Get partial results from a mongos if some shards are down (instead of throwing an error).
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"allowPartialResults"
=>
false
,
/**
* The number of documents to return per batch.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.batchSize/
*/
"batchSize"
=>
101
,
/**
* Attaches a comment to the query. If $comment also exists
* in the modifiers document, the comment field overwrites $comment.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"comment"
=>
""
,
/**
* Indicates the type of cursor to use. This value includes both
* the tailable and awaitData options.
* The default is NON_TAILABLE.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/comment/
*/
"cursorType"
=>
CursorType
::
NON_TAILABLE
,
/**
* The maximum number of documents to return.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.limit/
*/
"limit"
=>
0
,
/**
* The maximum amount of time to allow the query to run. If $maxTimeMS also exists
* in the modifiers document, the maxTimeMS field overwrites $maxTimeMS.
*
* @see http://docs.mongodb.org/manual/reference/operator/meta/maxTimeMS/
*/
"maxTimeMS"
=>
0
,
/**
* Meta-operators modifying the output or behavior of a query.
*
* @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
*/
"modifiers"
=>
array
(),
/**
* The server normally times out idle cursors after an inactivity period (10 minutes)
* to prevent excess memory use. Set this option to prevent that.
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"noCursorTimeout"
=>
false
,
/**
* Internal replication use only - driver should not set
*
* @see http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-query
*/
"oplogReplay"
=>
false
,
/**
* Limits the fields to return for all matching documents.
*
* @see http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/
*/
"projection"
=>
array
(),
/**
* The number of documents to skip before returning.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.skip/
*/
"skip"
=>
0
,
/**
* The order in which to return matching documents. If $orderby also exists
* in the modifiers document, the sort field overwrites $orderby.
*
* @see http://docs.mongodb.org/manual/reference/method/cursor.sort/
*/
"sort"
=>
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