MongoDB::createDBRef
(PECL mongo >=0.9.0)
MongoDB::createDBRef — Creates a database reference
Description
This method is a flexible interface for creating database refrences (see MongoDBRef).
Parameters
-
collection
-
The collection to which the database reference will point.
-
document_or_id
-
If an array or object is given, its _id field will be used as the reference ID. If a MongoId or scalar is given, it will be used as the reference ID.
Return Values
Returns a database reference array.
If an array without an _id field was provided as the
document_or_id parameter, NULL
will be returned.
Examples
Example #1 MongoDB::createDBRef() example
Example demonstrating how to programatically create a DB reference array from a document.
<?php
$articles = $db->articles;
$article = array(
'title' => 'Test article',
'description' => 'Test article description'
);
$articles->insert($article);
$ref = $db->createDBRef('articles', $article);
print_r($article);
print_r($ref);
?>
The above example will output something similar to:
Array ( [title] => Test article [description] => Test article description [_id] => MongoId Object ( ) ) Array ( [$ref] => articles [$id] => MongoId Object ( ) )
Now the $ref can be stored on another document and retrieved later with MongoDB::getDBRef() or MongoCollection::getDBRef().
Example #2 MongoDB::createDBRef() example
Example demonstrating how to programatically create a DB reference from just an id.
<?php
$id = new MongoId('47cc67093475061e3d9536d2');
$ref = $db->createDBRef('articles', $id);
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mongodb.createdbref.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.