Database Reference
In-Depth Information
Defining a document is not specifically related to MongoDB—instead, you create an array with keys and values
stored in it, as in the following example:
$contact = array(
"First Name" => "Philip",
"Last Name" => "Moran",
"Address" => array(
"Street" => "681 Hinkle Lake Road",
"Place" => "Newton",
"Postal Code" => "MA 02160",
"Country" => "USA"
)
,
"E-Mail" => array(
" pm@example.com " ,
" pm@office.com " ,
" philip@example.com " ,
" philip@office.com " ,
" moran@example.com " ,
" moran@office.com " ,
" pmoran@example.com " ,
" pmoran@office.com "
),
"Phone" => "617-546-8428",
"Age" => 60
);
Warning
Strings sent to the database need to be UtF-8 formatted to prevent an exception from occurring.
Once you've assigned your data properly to a variable—called $contact in this case—you can use the insert()
function to insert it in the MongoCollection class:
// Connect to the database
$c = new MongoClient();
// Select the collection 'people'
$collection = $c->contacts->people;
// Insert the document '$contact' into the people collection '$collection'
$collection->insert($contact);
The insert() function takes five options, specified in an array: fsync, j, w, wtimeout and timeout . The fsync
option can be set to TRUE or FALSE ; FALSE is the default value for this option. If set to TRUE , fsync forces the data to be
written to the hard disk before it indicates the insertion was a success. This option will override any setting for the
option w , setting it to 0. The j option can be set to TRUE or FALSE , where FALSE is the default. If set, the j option will
force the data to be written to the journal before indicating the insertion was a success. If you are unfamiliar with
journaling, think of it as a log file that keeps track of the changes made to your data, before it is finally written to disk.
This ensures that, were mongod to stop unexpectedly, it will be able to recover the changes written to the journal,
thereby preventing your data from entering an inconsistent state.
 
 
Search WWH ::




Custom Search