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
7b1696bb
Commit
7b1696bb
authored
Dec 11, 2014
by
Hannes Magnusson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Importing json, finding one document
parent
bb128ed0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
0 deletions
+105
-0
data.md
docs/data.md
+35
-0
usage.md
docs/usage.md
+49
-0
import-json.php
examples/import-json.php
+19
-0
mkdocs.yml
mkdocs.yml
+2
-0
No files found.
docs/data.md
0 → 100644
View file @
7b1696bb
# Example data
For the purpose of this documentation we will be using the
[
zips.json
](
http://media.mongodb.org/zips.json
)
in our examples.
Importing the dataset into the database can be done in several ways,
for example using the following script:
```
php
<?php
$file
=
"http://media.mongodb.org/zips.json"
;
$zips
=
file
(
$file
,
FILE_IGNORE_NEW_LINES
);
$batch
=
new
MongoDB\WriteBatch
(
true
);
foreach
(
$zips
as
$string
)
{
$document
=
json_decode
(
$string
);
$batch
->
insert
(
$document
);
}
$manager
=
new
MongoDB\Manager
(
"mongodb://localhost"
);
$result
=
$manager
->
executeWriteBatch
(
"examples.zips"
,
$batch
);
printf
(
"Inserted %d documents
\n
"
,
$result
->
getInsertedCount
());
?>
```
Outputs
```
Inserted 29353 documents
```
docs/usage.md
0 → 100644
View file @
7b1696bb
# Usage
# MongoDB\Collection
MongoDB
\C
ollection is the main object of this component.
It has convenience methods for most of the _usual suspects_ tasks, such as inserting
documents, querying, updating, counting, and so on.
Constructing a MongoDB
\C
ollection requires a MongoDB
\M
anager and then namespace to operate
on. A
[
MongoDB namespace
](
http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace
)
is in the form of "databaseName.collectionName", for example:
`examples.zips`
.
A
[
WriteConcern
](
http://docs.mongodb.org/manual/core/write-concern/
)
and
[
ReadPreference
](
http://docs.mongodb.org/manual/core/read-preference/
)
can also optionally
be provided, if omitted the default values from the MongoDB
\M
anager will be used.
## finding a specific document
```
<?php
require __DIR__ . "/../". "vendor/autoload.php";
$manager = new MongoDB\Manager("mongodb://localhost:27017");
$collection = new MongoDB\Collection($manager, "examples.zips");
$sunnyvale = $collection->findOne(array("_id" => "94086"));
var_dump($sunnyvale);
?>
```
Outputs
```
array(5) {
["_id"]=>
string(5) "94086"
["city"]=>
string(9) "SUNNYVALE"
["loc"]=>
array(2) {
[0]=>
float(-122.023771)
[1]=>
float(37.376407)
}
["pop"]=>
int(56215)
["state"]=>
string(2) "CA"
}
```
examples/import-json.php
0 → 100644
View file @
7b1696bb
<?php
$file
=
"http://media.mongodb.org/zips.json"
;
$zips
=
file
(
$file
,
FILE_IGNORE_NEW_LINES
);
$batch
=
new
MongoDB\WriteBatch
(
true
);
foreach
(
$zips
as
$string
)
{
$document
=
json_decode
(
$string
);
$batch
->
insert
(
$document
);
}
$manager
=
new
MongoDB\Manager
(
"mongodb://localhost"
);
$result
=
$manager
->
executeWriteBatch
(
"examples.zips"
,
$batch
);
printf
(
"Inserted %d documents
\n
"
,
$result
->
getInsertedCount
());
?>
mkdocs.yml
View file @
7b1696bb
...
...
@@ -3,5 +3,7 @@ repo_url: https://github.com/bjori/phongo-crud
theme
:
spacelab
pages
:
-
[
index.md
,
Home
]
-
[
usage.md
,
Usage
]
-
[
data.md
,
Example data
]
extra_javascript
:
-
pretty.js
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