开发者

How to use mongoID's

Hi I'm making a web app using mongoDB for the persistence later, I was wondering how you could use the mongo ID automatically generated to point to a common row in a collection. For example we have a users collection (with an '_id' field) and another collection to store t开发者_JAVA技巧he user's API key information. How would it be possible to link these two so that the '_id' in the API key collection corresponds to the user collection so that they point to the right user. This is being done in PHP 5.

Thanks in advance, RayQuang


You can overwrite the default id with your own id. So when you do an insert, you can set "_id" in your object, equivalent to the mongoid present in your API key collection.

example code :

<?php

$objectFromAPICollection =  find_object_from_API_collection;

$userid = $objectFromApiCollection["_id"] ;

$theObjId = new MongoId($userid);

$connection = new Mongo();

$objectToInsert = array( "_id" => $theObjId , "data" => $someData );

// this will return our matching entry.
$connection->selectDB('someDb')->selectCollection('users')->insert($objectToInsert);

?>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜