tags:

views:

26

answers:

1

Please note, "user_id" in my plans collection is NOT an object_id. I have it stored in the plans collection to reference the user's _id in the user_accounts collection. I thought about storing usernames across all collections in order to reference the user, but that wouldn't be idea should the user wishes to change his/her username.

// Retrieve User ID
$query = array("username" => $user_id);
$fields = array("_id");
$user = $collection_user->findOne($query, $fields);

// Retrieve plans made by user
$query = array("user_id" => $user['_id']);
$fields = array("plan_title");
$data = $collection_plans->find($query, $fields);

If I hardcode the _id into the query it works fine as follows:

// Retrieve plans made by user
$query = array("user_id" => "4cc1790f6c0d49bf9424fc73");
$fields = array("plan_title");
$data = $collection_plans->find($query, $fields);
+1  A: 

Looks like I had to convert it into a string.

$uid = $user['_id'] . "";
luckytaxi
I think you're correct. The value of $user['_id'] should be an instance of MongoId, and not a string. Note a "cleaner" way of converting to a string woule be $uid = (string)$user['_id'];
mellowsoon