简体   繁体   English

php中mongodb查询的结果集(子文档)

[英]Result set(Sub Documents) for a mongodb query in php

I am trying to get the result set output for a query. 我正在尝试获取查询的结果集输出。 I was able to write the query and am getting the required result using var_dump. 我能够编写查询,并使用var_dump获得所需的结果。

$comment = array( 
               "Name" => array( 'FirstName' => $n1, 'LastName' => $n2 ) 
                );                                                                                  
$cursor= $c_users->find( $comment );    
$result = $cursor->getNext();
echo var_dump( $result );

But I am failing when i want to get output for a sub document. 但是我想获取子文档的输出时失败了。 In case of a normal document the below code works: 对于普通文档,以下代码适用:

echo $result['variable'];

But consider a case where there is a document structure given below: 但是考虑以下给出的文档结构的情况:

array( 
      "Name" => array( 'FirstName' => $n1, 'LastName' => $n2 ) 
     );

Could anyone help me with the syntax to output the first name. 谁能用语法帮助我输出名字。 I was trying with below code but did not succeed. 我正在尝试使用以下代码,但未成功。

echo $result['variable.FirstName']

So the question is how to access embedded document of mongodb using PHP? 那么问题是如何使用PHP访问mongodb的嵌入式文档?

Thanks and Regards, Sai 感谢和问候,赛

In order to access embedded document key (property) you need to treat embedded document like an array in PHP dot notation won't work in PHP for MongoDB so you should try: 为了访问嵌入式文档密钥(属性),您需要将嵌入式文档当作PHP点表示法中的数组对待,在MongoDB的PHP中不起作用,因此您应该尝试:

echo $result['Name']['FirstName'];

Accessing embedded document with dot notation will work in mongoshell not in PHP driver. 使用点符号访问嵌入式文档将在mongoshell中而不是在PHP驱动程序中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM