使用mongoose查询ObjectIds数组

时间:2015-12-18 05:11:58

标签: mongodb mongoose

users表包含一个items数组,它只是Item ObjectId的集合。

用户

<?php
include_once 'dbconfig.php';
$first_name= null;
$last_name=null;
$employee_nrc=null;
$phone_no=null;
$business_type=null;
$business_site=null;
$business_activity=null;

if(isset($_POST['btn-update']))
{
    $id = $_POST['edit_id'];
    $fname = $_POST['first_name'];
    $lname = $_POST['last_name'];
    $employee_nrc = $_POST['employee_nrc'];
    $Phone = $_POST['phone_no'];
    $Businesstype = $_POST['business_type'];
    $Businesssite = $_POST['business_site'];
    $Businessactivity = $_POST['business_activity'];


    if($crud->update($id,$fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity))
    {
        $msg = "<div class='alert alert-info'>
                <strong>WOW!</strong> Record was updated successfully <a href='index.php'>HOME</a>!
                </div>";
    }
    else
    {
        $msg = "<div class='alert alert-warning'>
                <strong>SORRY!</strong> ERROR while updating record !
                </div>";
    }
}

if(isset($_GET['edit_id']))
{
    $id = $_GET['edit_id'];
    extract($crud->getID($id)); 
}

?>
<?php include_once 'header.php'; ?>

<div class="clearfix"></div>

<div class="container">
<?php
if(isset($msg))
{
    echo $msg;
}
?>
</div>

<div class="clearfix"></div><br />

<div class="container">

     <form class="form-horizontal" method='post'>
  <fieldset>
    <legend>Registration System</legend>
    <div class="form-group">
      <label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
      <div class="col-lg-10">
        <input type="text" name="first_name" class="form-control" value="<?php echo $first_name; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
      <div class="col-lg-10">

        <input type="text" name="last_name" class="form-control" value="<?php echo $last_name; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeeNRC" class="col-lg-2 control-label">Employee NRC</label>
      <div class="col-lg-10">
        <input type="text" name="employee_nrc" class="form-control" value="<?php echo $employee_nrc; ?>" required>
      </div>
    </div>
    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Employee Phone Number</label>
      <div class="col-lg-10">
        <input type="text" name="phone_no" class='form-control' value="<?php echo $phone_no; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Type</label>
      <div class="col-lg-10">
        <input type="text" name="business_type" class='form-control' value="<?php echo $business_type; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Location</label>
      <div class="col-lg-10">
        <input type="text" name="business_site" class='form-control' value="<?php echo $business_site; ?>" required>
      </div> 
    </div>

    <div class="form-group">
      <label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Activities</label>
      <div class="col-lg-10">
        <input type="text" name="business_activity" class='form-control' value="<?php echo $business_activity; ?>" required>
      </div> 
    </div>


    <div class="form-group">
      <div class="col-lg-10 col-lg-offset-2">
        <button type="submit" class="btn btn-primary" name="btn-update">
                <span class="glyphicon glyphicon-edit"></span>  Update this Record
                </button>
                <a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> &nbsp; CANCEL</a>
      </div>
    </div>
  </fieldset>
</form>


</div>

<?php include_once 'footer.php'; ?>

{
  state: 'active',
  items: [ObjectId("4ecc05e55dd98a436ddcc47c"), ObjectId("4ecc05e55dd98a436ddcc47d")]
}

我想编写一个查询,查找所有拥有带创建者&#39;标记&#39;的项目的用户。这可能在一个查询中?我试过这样的东西,但它不起作用。

{
 name: 'weapon',
 creator: 'mark'
}

1 个答案:

答案 0 :(得分:0)

首先,您需要将项目集合作为嵌入或引用。考虑到这种结构,我们可以将查询编写为

db.Users.find({“items.creator”:'mark'})