Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb和PHP:如何在不使用键名的情况下查询嵌套数组_Php_Mongodb - Fatal编程技术网

Mongodb和PHP:如何在不使用键名的情况下查询嵌套数组

Mongodb和PHP:如何在不使用键名的情况下查询嵌套数组,php,mongodb,Php,Mongodb,我可能在这里遗漏了一些简单的东西,但我似乎找不到一种方法来构建一个查询,该查询允许我更新一组嵌套值中的匹配项 我有一个这样的文档,用于我一直在使用的博客应用程序(目前使用MySQL): 我希望能够建立一个查询,可以搜索匹配的“评论标题”,然后允许我根据需要更新/更改/删除数据 显然,我可以使用包含键值的查询执行更新。类似这样的工作原理: $collection->update( array("post-comments.0.comment-title" => $_POST['

我可能在这里遗漏了一些简单的东西,但我似乎找不到一种方法来构建一个查询,该查询允许我更新一组嵌套值中的匹配项

我有一个这样的文档,用于我一直在使用的博客应用程序(目前使用MySQL):

我希望能够建立一个查询,可以搜索匹配的“评论标题”,然后允许我根据需要更新/更改/删除数据

显然,我可以使用包含键值的查询执行更新。类似这样的工作原理:

$collection->update(
    array("post-comments.0.comment-title" => $_POST['comment-title']),
    array('$set' => array('entries.0' => array('comment-title' => $_POST['comment-title'], 'comment-body' => $_POST['comment-body'], 'owner' => $_SESSION['username'], 'updated' => gmdate('Ymd\-His O')))));
但我希望我遗漏了一些东西,这些东西允许我省略键,并且仍然能够基于值(在本例中为“注释标题”)匹配其中一个嵌套数组

无论如何,对不起,这可能不是最好的例子,我可能最终会使用注释中的键来识别它们(注释#),但由于嵌套和创建相当复杂的对象似乎是MongoDB的一些优点,我只是希望有人能指出我可能遗漏了什么


一个删除或更新特定用户(比如博客作者刚刚被列入黑名单的用户)所有评论的查询可能是一个更好的例子。除了取出整个文档,然后使用PHP迭代嵌套数组之外,我不知道该怎么做。

试试。。。注意,我删除了“密钥”


干杯

我目前没有可用的环境,但是当运行$collection->findOne(数组(“post comments.comment title”=>$\u post['comment-title'])时会发生什么,当你回家后,以答案的形式添加你的评论,我会告诉你重点。按照你上面的建议添加你的查询解决了问题。我知道这是一个令人头痛的问题,但非常感谢你的帮助。工作查询/更新现在看起来像:$collection->update(数组(“post comments.comment title”=>$\u post['comment-title']、数组('$set'=>array('post-comments.$'=>array('comment-title'=>$\u-post['comment-title']、'comment-body'=>$\u-post['comment-body']、'owner'=>u-SESSION['username']、'updated'=>gmdate('Ymd-His-O''));而且工作起来很有魅力。再次感谢幸运的出租车。@earlz检查他在答案上方对我的问题所做的评论。他建议了一个没有关键字名称的查询,效果非常好。目前MongoDB文档中似乎缺少此信息,但问题出现在搜索中-我的问题与我预期的相同ng必须在我的查询中使用数组索引。
$collection->update(
    array("post-comments.0.comment-title" => $_POST['comment-title']),
    array('$set' => array('entries.0' => array('comment-title' => $_POST['comment-title'], 'comment-body' => $_POST['comment-body'], 'owner' => $_SESSION['username'], 'updated' => gmdate('Ymd\-His O')))));
$collection->update(array("post-comments.comment-title" ...