doupo5861 2016-03-31 18:28
浏览 101
已采纳

PHP mongoDB列出具有相同字段值的用户

Using PHP and MongoClient, I'm trying to figure out how to list users in one organization.

I can list all users from all organizations, but I only want to list users from one organization.

How do I filter the documents like this?

My document structure is simple:

{
    "_id" : ObjectId("56fd6a77768d8864028b4578"),
    "name" : "Jack Daniels",
    "email" : "jackdaniels@janes-jeans.com",
    "manager" : "John doe",
    "password" : "$2y$10$cec5pzCghhhRdwPVnT/t4.EssfIbqighVqpZSa3jWPRV4gdfXUGDu",
    "organization" : "Jane's Jeans"
}

Here is my code where I'm trying to only get results related to one organization:

<optgroup label="Employees">
<?php
     $cursor = $collection->find($curr_org);
     foreach($cursor as $curr_org) {
          echo '<option name="name">';
               echo $curr_org['name'];
          echo '</option>';
     }
?>
</optgroup>

$curr_org contains the organization name.

Removing $curr_org from find() displays all user from all organizations.

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • dpmp9359 2016-04-01 08:58
    关注

    You need to pass proper argument to ->find:

    $cursor = $collection->find(['organization' => $curr_org]);
    

    For more details you can check out documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题