MongoDB Native DriverMongo
+ &reference.mongo.setup;
+
+
&reference.mongo.manual;
&reference.mongo.core;
&reference.mongo.types;
&reference.mongo.gridfs;
&reference.mongo.misc;
- &reference.mongo.functions;
+ &reference.mongo.reference;
&reference.mongo.exceptions;
diff --git a/reference/mongo/connecting.xml b/reference/mongo/connecting.xml
index bd13f5f254..abc6eabbb9 100644
--- a/reference/mongo/connecting.xml
+++ b/reference/mongo/connecting.xml
@@ -1,7 +1,7 @@
-
+Connecting
@@ -359,4 +359,25 @@ for ($i=0; $i<1000; $i++) {
-
+
+
+
diff --git a/reference/mongo/functions.xml b/reference/mongo/functions.xml
deleted file mode 100644
index 40dbec35ca..0000000000
--- a/reference/mongo/functions.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
-
- Functions
-
- &reference.mongo.reference;
-
-
diff --git a/reference/mongo/manual.xml b/reference/mongo/manual.xml
index 73bddcdd2b..cc1b9fd9fa 100644
--- a/reference/mongo/manual.xml
+++ b/reference/mongo/manual.xml
@@ -1,16 +1,17 @@
-
+Manual
-
- This manual goes into some detail about how to use MongoDB, but it mostly
- covers using the PHP driver. For information about how to design a
- schema, what terms means, and setting up the database server, check out the
- MongoDB documentation.
-
+
+
+ This manual goes into some detail about how to use MongoDB, but it mostly
+ covers using the PHP driver. For information about how to design a
+ schema, what terms means, and setting up the database server, check out the
+ MongoDB documentation.
+
+
- &reference.mongo.configure;
&reference.mongo.tutorial;
&reference.mongo.sqltomongo;
&reference.mongo.connecting;
@@ -18,12 +19,11 @@
&reference.mongo.writes;
&reference.mongo.queries;
&reference.mongo.updates;
- &reference.mongo.ini;
&reference.mongo.security;
&reference.mongo.trouble;
&reference.mongo.testing;
-
+
-
+Querying
@@ -300,7 +300,7 @@ $cursor = $collection->find(array("awards" => array('$in' => array("gold", "copp
-
+
-
+Read Preferences
MongoDB 2.2 and version 1.3.0 of the driver add support for
@@ -279,4 +279,26 @@ $m->setReadPreference(MongoClient::RP_NEAREST, array(
-
+
+
+
+
diff --git a/reference/mongo/security.xml b/reference/mongo/security.xml
index 77bd5373e2..6f16d6fad0 100644
--- a/reference/mongo/security.xml
+++ b/reference/mongo/security.xml
@@ -1,7 +1,7 @@
-
+Security
@@ -143,7 +143,7 @@ $db->execute(new MongoCode("eval(input);", $scope));
user input as code.
-
+
+
+
+ &reftitle.setup;
+
+
+
+ &reftitle.required;
+ &no.requirement;
+
+
+
+
+ &reference.mongo.configure;
+
+
+
+ &reference.mongo.ini;
+
+
+
+
+
+
diff --git a/reference/mongo/sqltomongo.xml b/reference/mongo/sqltomongo.xml
index 85f7bd3db2..7304bdb4d0 100644
--- a/reference/mongo/sqltomongo.xml
+++ b/reference/mongo/sqltomongo.xml
@@ -1,6 +1,6 @@
-
+SQL to Mongo Mapping Chart
This is a PHP-specific version of the
@@ -230,7 +230,7 @@
-
+
-
+Running the Driver's Tests
@@ -96,4 +96,26 @@ $ phpunit tests/MongoSuite.php
type testing any functionality.
-
+
+
+
+
diff --git a/reference/mongo/trouble.xml b/reference/mongo/trouble.xml
index 31a45a69d7..4bb913e5fb 100644
--- a/reference/mongo/trouble.xml
+++ b/reference/mongo/trouble.xml
@@ -1,7 +1,7 @@
-
+Troubleshooting
@@ -60,7 +60,7 @@ $ ./configure CFLAGS=-DDEBUG
-
+
-
+Tutorial
-
- Introduction
-
- This is the 10gen-supported PHP driver for MongoDB.
-
- Here's a quick code sample that connects, inserts documents, queries for
- documents, iterates through query results, and disconnects from MongoDB.
- There are more details on each step in the tutorial below.
-
+
+ This is the 10gen-supported PHP driver for MongoDB.
+
+ Here's a quick code sample that connects, inserts documents, queries for
+ documents, iterates through query results, and disconnects from MongoDB.
+ There are more details on each step in the tutorial below.
+
+
]]>
-
- This would output:
-
+ &example.outputs;
-
-
+
+
+
+ Making a Connection
To connect to the database server, use one of the following:
-
+
+
]]>
-
+
+
You do not have to explicitly disconnect from the database. When
$connection goes out of scope, the connection will be
closed automatically and any database resources it was using will be freed.
-
+ See Also
The chapter on connecting
@@ -88,13 +89,15 @@ $connection = new MongoClient( "example.com:65432" ); // connect to a remote hos
all possible options with a number of examples.
+
-
+ Getting a Database
To select a database, use:
-
+
+
dbname;
?>
]]>
-
+
+
The database does not need to be created in advance, you can create new
databases by selecting them.
@@ -111,7 +115,8 @@ $db = $connection->dbname;
Be careful of typos! You can inadvertently create a new database, which can
cause confusing errors:
-
+
+
mybiglongdbanme;
?>
]]>
-
+
+
-
+ See Also
The API documentation on the MongoDB class contains
@@ -133,12 +139,13 @@ $db = $connection->mybiglongdbanme;
-
+ Getting A Collection
Getting a collection has the same syntax as getting a database:
-
+
+
baz->foobar;
?>
]]>
-
+
+
A collection is analogous to a table (if you are familiar with relational
databases).
-
+ See Also
The API documentation on the MongoCollection class
@@ -164,13 +172,14 @@ $collection = $connection->baz->foobar;
-
+ Inserting a Document
Associative arrays are the basic object that can be saved to a collection in
the database. A somewhat random "document" might be:
-
+
+
"MongoDB",
?>
]]>
-
+
+
Note that you can have nested arrays and objects.
To insert this document, use MongoCollection::insert:
-
+
+
insert( $doc );
?>
]]>
-
-
+
+
+ See Also
The API documentation on MongoCollection::insert
@@ -208,7 +220,7 @@ $collection->insert( $doc );
-
+
Finding Documents using MongoCollection::findOne
@@ -218,7 +230,8 @@ $collection->insert( $doc );
single document from the collection. This method is useful when there only is
one document matching the query or you are only interested in one result.
-
+
+
]]>
-
-
- This should print:
-
-
+
+ &example.outputs;
+
@@ -263,7 +274,8 @@ array(6) {
}
}
]]>
-
+
+
Note that there is an _id field that has been added
automatically to your document. _id is the "primary key"
@@ -274,7 +286,8 @@ array(6) {
If you specify your own _id field, it must be unique to
the collection. See the example here:
-
+
+
bar->insert(array("_id" => 1), array("safe" => true));
?>
]]>
-
+
+
Note that these inserts pass a second array:
array("safe" => true). This second field specifies
@@ -298,7 +312,7 @@ $db->bar->insert(array("_id" => 1), array("safe" => true));
through. In general, all writes should use the "safe" option (it is omitted
in previous examples for simplicity).
-
+ See AlsoMongoCollection::findOne for more information about
@@ -317,7 +331,7 @@ $db->bar->insert(array("_id" => 1), array("safe" => true));
-
+ Adding Multiple Documents
In order to do more interesting things with queries, let's add multiple
@@ -325,7 +339,8 @@ $db->bar->insert(array("_id" => 1), array("safe" => true));
array( "i" => value ); and we
can do this fairly efficiently in a loop:
-
+
+
]]>
-
+
+
Notice that we can insert arrays with different key sets into the same
@@ -344,13 +360,14 @@ for($i=0; $i<100; $i++) {
-
+ Counting Documents in A Collection
Now that we've inserted 101 documents (the 100 we did in the loop, plus the
first one), we can check to see if we have them all using the
MongoCollection::count method.
-
+
+
count();
?>
]]>
-
+
+
and it should print 101.
-
+ Using a Cursor to Get All of the Documents
In order to get all the documents in the collection, we will use
@@ -372,7 +390,8 @@ echo $collection->count();
set of documents that matched our query. So to query all of the documents and
print them out:
-
+
+
$value) {
?>
]]>
-
+
+
and that should print all 101 documents in the collection.
$id is the _id field of a document
(cast to a string) and $value is the document itself.
-
+ See Also
The API documentation on MongoCollection::find
@@ -399,7 +419,7 @@ foreach ($cursor as $id => $value) {
-
+ Setting Criteria for a Query
We can create a query to pass to the
@@ -407,7 +427,8 @@ foreach ($cursor as $id => $value) {
documents in our collection. For example, if we wanted to find the document
for which the value of the "i" field is 71, we would do the following:
-
+
+
hasNext() ) {
?>
]]>
-
-
- and it should just print just one document:
-
-
+
+ &example.outputs;
+
@@ -436,16 +455,18 @@ array(2) {
"testCollection"
}
]]>
-
+
+
-
+ Getting A Set of Documents With a Query
We can use the query to get a set of documents from our collection. For
example, if we wanted to get all documents where "i" > 50, we could write:
-
+
+
hasNext() ) {
?>
]]>
-
-
- which should print the documents where i > 50. We could also get a range, say
- 20 < i <= 30:
-
-
+
+
+ which should print the documents where i > 50. We could also get a range, say
+ 20 < i <= 30:
+
+
hasNext() ) {
?>
]]>
-
+
+
Remember to always escape the $-symbol or use single quotes. Otherwise PHP
will interpret it to be the variable $gt.
-
+ Creating An Index
MongoDB supports indexes, and they are very easy to add on a collection. To
create an index, you specify the field name and direction: ascending (1) or
descending (-1). The following creates an ascending index on the "i" field:
-
+
+
ensureIndex( array( "i" => -1, "j" => 1 ) ); // index on "i" descending,
?>
]]>
-
+
+
Indexing is critical for good read performance as your data grows. If you
are not familiar with indexing, check out the
@@ -507,5 +531,25 @@ $coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // index on "i" descending,
MongoDB indexing documentation.
-
-
+
+
+
diff --git a/reference/mongo/updates.xml b/reference/mongo/updates.xml
index 7a0a8eeafe..22335e904c 100644
--- a/reference/mongo/updates.xml
+++ b/reference/mongo/updates.xml
@@ -1,7 +1,7 @@
-
+Updates
@@ -138,7 +138,7 @@ $blog->update(
]]>
-
+
-
+Writes
@@ -136,7 +136,7 @@ $blog->update(
]]>
-
+