从Mongo Cursor获取第一个对象

时间:2012-06-21 22:57:50

标签: php mongodb

我正在查询MongoDB,我只想要第一个对象。我知道我可以使用findOne,但在我出错的地方我仍然感到困惑。

这不起作用:

if ($cursor->count() > 0) {
    $image = $cursor->current();
    // neither does this work
    // $image = $cursor[0]; 
    return $image;
} else {
    return false;
}   

//echo $image->filename;
// Throws error: Trying to access property of non-object image

但这有效:

if ($cursor->count() > 0) {
    $image = null;
    foreach($cursor as $obj)
        $image = $obj;
    return $image;
} else {
    return false;
}   

2 个答案:

答案 0 :(得分:15)

这个怎么样:

if ($cursor->count() > 0) {
    $cursor->next();
    $image = $cursor->current();
    return $image;
} else {
    return false;
}

奖金:引自the Doc page

  

公共数组MongoCursor :: current(void)
这将返回NULL直到   调用MongoCursor :: next()。

答案 1 :(得分:0)

raina77ow提供的解决方案使用标记为遗留的Mongo库。

目前只有一种方法可以从游标中获取第一个元素 - 使用MongoDB\Driver\Cursor::toArray方法:

<?xml version="1.0" encoding="UTF-8"?><process version="7.5.000">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="7.5.000" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="generate_data" compatibility="7.5.000" expanded="true" height="68" name="Generate Data" width="90" x="45" y="34">
        <parameter key="number_examples" value="10"/>
        <parameter key="number_of_attributes" value="10"/>
      </operator>
      <operator activated="true" class="select_attributes" compatibility="7.5.000" expanded="true" height="82" name="Select Attributes" width="90" x="179" y="34">
        <parameter key="attribute_filter_type" value="single"/>
        <parameter key="attribute" value="label"/>
        <parameter key="invert_selection" value="true"/>
        <parameter key="include_special_attributes" value="true"/>
        <description align="center" color="transparent" colored="false" width="126">remove label for clarity</description>
      </operator>
      <operator activated="true" class="loop_examples" compatibility="7.5.000" expanded="true" height="103" name="Loop Examples" width="90" x="313" y="34">
        <process expanded="true">
          <operator activated="true" class="filter_example_range" compatibility="7.5.000" expanded="true" height="82" name="Filter Example Range" width="90" x="45" y="34">
            <parameter key="first_example" value="%{example}"/>
            <parameter key="last_example" value="%{example}"/>
            <description align="center" color="transparent" colored="false" width="126">only work on current example</description>
          </operator>
          <operator activated="true" class="transpose" compatibility="7.5.000" expanded="true" height="82" name="Transpose" width="90" x="179" y="34">
            <description align="center" color="transparent" colored="false" width="126">transpose the row to columns with attribute name and attribute value</description>
          </operator>
          <operator activated="true" class="rename" compatibility="7.5.000" expanded="true" height="82" name="Rename" width="90" x="313" y="34">
            <parameter key="old_name" value="id"/>
            <parameter key="new_name" value="Example%{example}AttributeName"/>
            <list key="rename_additional_attributes">
              <parameter key="att_1" value="Value"/>
            </list>
            <description align="center" color="transparent" colored="false" width="126">rename for clarity, include original &amp;quot;row&amp;quot; number in attribute name</description>
          </operator>
          <operator activated="true" class="sort" compatibility="7.5.000" expanded="true" height="82" name="Sort" width="90" x="447" y="34">
            <parameter key="attribute_name" value="Value"/>
            <parameter key="sorting_direction" value="decreasing"/>
            <description align="center" color="transparent" colored="false" width="126">sort decreasing by attribute value</description>
          </operator>
          <operator activated="true" class="filter_example_range" compatibility="7.5.000" expanded="true" height="82" name="Filter Example Range (2)" width="90" x="581" y="34">
            <parameter key="first_example" value="1"/>
            <parameter key="last_example" value="5"/>
            <description align="center" color="transparent" colored="false" width="126">keep 5 attributes with biggest values</description>
          </operator>
          <connect from_port="example set" to_op="Filter Example Range" to_port="example set input"/>
          <connect from_op="Filter Example Range" from_port="example set output" to_op="Transpose" to_port="example set input"/>
          <connect from_op="Transpose" from_port="example set output" to_op="Rename" to_port="example set input"/>
          <connect from_op="Rename" from_port="example set output" to_op="Sort" to_port="example set input"/>
          <connect from_op="Sort" from_port="example set output" to_op="Filter Example Range (2)" to_port="example set input"/>
          <connect from_op="Filter Example Range (2)" from_port="example set output" to_port="output 1"/>
          <portSpacing port="source_example set" spacing="0"/>
          <portSpacing port="sink_example set" spacing="0"/>
          <portSpacing port="sink_output 1" spacing="0"/>
          <portSpacing port="sink_output 2" spacing="0"/>
        </process>
        <description align="center" color="transparent" colored="false" width="126">Loop over all examples, current index is stored in the macro &amp;quot;example&amp;quot;</description>
      </operator>
      <connect from_op="Generate Data" from_port="output" to_op="Select Attributes" to_port="example set input"/>
      <connect from_op="Select Attributes" from_port="example set output" to_op="Loop Examples" to_port="example set"/>
      <connect from_op="Loop Examples" from_port="output 1" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
    </process>
  </operator>
</process>