Database Reference
In-Depth Information
The w option can be used to acknowledge or unacknowledge a write operation (making this option also
applicable for remove() and update() operations). If w is set to 0, the write operation will not be acknowledged; set it
to 1 and the write will be acknowledged by the (primary) server. When working with replica sets, w can also be set
to n , ensuring that the primary server acknowledges the write operation when successfully replicated to n nodes.
w can also be set to 'majority' —a reserved string—ensuring that the majority of the replica set will acknowledge the
write, or to a specific tag, ensuring that those tagged nodes will acknowledge the write. For this option also, the default
setting is 1. The wtimeout option can be used to specify how long the server is to wait for receiving acknowledgement
(in milliseconds). By default, this option is set to 10000. Lastly, the timeout option allows you to specify how long
(in milliseconds) the client needs to wait for a response from the database.
The following example illustrates how to use the w and wtimeout options to insert data:
// Define another contact
$contact = array(
"Fir't Name" => "Victoria",
"Last Name" => "Wood",
"Address" => array(
"Street" => "50 Ash lane",
"Place" => "Ystradgynlais",
"Postal Code" => "SA9 6XS",
"Country" => "UK"
)
,
"E-Mail" => array(
" vw@example.com " ,
" vw@office.com "
),
"Phone" => "078-8727-8049",
"Age" => 28
);
// Connect to the database
$c = new MongoClient();
// Select the collection 'people'
$collection = $c->contacts->people;
// Specify the w and wtimeout options
$options = array("w" => 1, "wtimeout" => 5000);
// Insert the document '$contact' into the people collection '$collection'
$collection->insert($contact,$options);
And that's all there is to inserting data into your database with the PHP driver. For the most part, you will
probably be working on defining the array that contains the data, rather than injecting the data into the array.
Listing Your Data
Typically, you will use the find() function to query for data. It takes a parameter that you use to specify your search
criteria; once you specify your criteria, you execute find() to get the results. By default, the find() function simply
returns all documents in the collection. This is similar to the shell examples discussed in Chapter 4. Most of the time,
however, you will not want to do this. Instead, you will want to define specific information to return results for. The
next sections will cover commonly used options and parameters that you can used with the find() function to filter
your results.
 
Search WWH ::




Custom Search