Skip to content

Commit 7b1696b

Browse files
committed
Importing json, finding one document
1 parent bb128ed commit 7b1696b

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

docs/data.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Example data
2+
3+
For the purpose of this documentation we will be using the
4+
[zips.json](http://media.mongodb.org/zips.json) in our examples.
5+
6+
Importing the dataset into the database can be done in several ways,
7+
for example using the following script:
8+
9+
```php
10+
<?php
11+
12+
$file = "http://media.mongodb.org/zips.json";
13+
14+
$zips = file($file, FILE_IGNORE_NEW_LINES);
15+
16+
17+
$batch = new MongoDB\WriteBatch(true);
18+
foreach($zips as $string) {
19+
$document = json_decode($string);
20+
$batch->insert($document);
21+
}
22+
23+
$manager = new MongoDB\Manager("mongodb://localhost");
24+
25+
$result = $manager->executeWriteBatch("examples.zips", $batch);
26+
printf("Inserted %d documents\n", $result->getInsertedCount());
27+
28+
?>
29+
```
30+
31+
Outputs
32+
33+
```
34+
Inserted 29353 documents
35+
```

docs/usage.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Usage
2+
3+
4+
# MongoDB\Collection
5+
6+
MongoDB\Collection is the main object of this component.
7+
It has convenience methods for most of the _usual suspects_ tasks, such as inserting
8+
documents, querying, updating, counting, and so on.
9+
10+
Constructing a MongoDB\Collection requires a MongoDB\Manager and then namespace to operate
11+
on. A [MongoDB namespace](http://docs.mongodb.org/manual/faq/developers/#faq-dev-namespace)
12+
is in the form of "databaseName.collectionName", for example: `examples.zips`.
13+
A [WriteConcern](http://docs.mongodb.org/manual/core/write-concern/) and
14+
[ReadPreference](http://docs.mongodb.org/manual/core/read-preference/) can also optionally
15+
be provided, if omitted the default values from the MongoDB\Manager will be used.
16+
17+
18+
## finding a specific document
19+
```
20+
<?php
21+
require __DIR__ . "/../". "vendor/autoload.php";
22+
23+
$manager = new MongoDB\Manager("mongodb://localhost:27017");
24+
$collection = new MongoDB\Collection($manager, "examples.zips");
25+
$sunnyvale = $collection->findOne(array("_id" => "94086"));
26+
var_dump($sunnyvale);
27+
28+
?>
29+
```
30+
Outputs
31+
```
32+
array(5) {
33+
["_id"]=>
34+
string(5) "94086"
35+
["city"]=>
36+
string(9) "SUNNYVALE"
37+
["loc"]=>
38+
array(2) {
39+
[0]=>
40+
float(-122.023771)
41+
[1]=>
42+
float(37.376407)
43+
}
44+
["pop"]=>
45+
int(56215)
46+
["state"]=>
47+
string(2) "CA"
48+
}
49+
```

examples/import-json.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
$file = "http://media.mongodb.org/zips.json";
4+
5+
$zips = file($file, FILE_IGNORE_NEW_LINES);
6+
7+
8+
$batch = new MongoDB\WriteBatch(true);
9+
foreach($zips as $string) {
10+
$document = json_decode($string);
11+
$batch->insert($document);
12+
}
13+
14+
$manager = new MongoDB\Manager("mongodb://localhost");
15+
16+
$result = $manager->executeWriteBatch("examples.zips", $batch);
17+
printf("Inserted %d documents\n", $result->getInsertedCount());
18+
19+
?>

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ repo_url: https://github.com/bjori/phongo-crud
33
theme: spacelab
44
pages:
55
- [index.md, Home]
6+
- [usage.md, Usage]
7+
- [data.md, Example data]
68
extra_javascript:
79
- pretty.js

0 commit comments

Comments
 (0)