请注意,我的计划集中的“ user_id”不是object_id.我将其存储在计划集合中,以在user_accounts集合中引用用户的_id.我曾考虑过在所有集合中存储用户名以引用用户,但是如果用户希望更改其用户名就不知道了.
// 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);
如果我将_id硬编码到查询中,则可以按以下方式正常工作:
// Retrieve plans made by user
$query = array("user_id" => "4cc1790f6c0d49bf9424fc73");
$fields = array("plan_title");
$data = $collection_plans->find($query, $fields);
解决方法:
看起来我不得不将其转换为字符串.
$uid = $user['_id'] . "";