开发者

How to pull out Object ID in mongodb and search against it?

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 = $c开发者_如何学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);


Looks like I had to convert it into a string.

$uid = $user['_id'] . "";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜