使用 PHP 在 MongoDB 中的 "array of objects" 字段上获取条件文档

Fetch documents with condition on "array of objects" field in MongoDB with PHP

我看到其他类似的查询数组字段的问题,但其中大部分都是针对 Mongo 的,我无法将解决方案转换为 PHP 的驱动程序 'find()'。

我也发现了一个类似的问题here但是我无法用它来解决我的问题

PHP driver's documentation 也没有帮助,有一个 find() 示例,但字段不是像我的数组。

给我带来麻烦的字段是“Sentenca”,它是一个包含“Situacao”和“Prob”的数组。像下面这样:

"Sentenca": [{
        "Situacao": "nc",
        "Prob": 0.96
        }]

“Sentenca”总是只包含一个索引 [0] 与这两个字段。

我需要查询“Situacao”不是“nc”的所有文档。我所知道的是我应该使用运算符 $nin 但我不知道如何使用 find() 来做到这一点。

我得到的最远的是这个(这个查询没有做我需要的):

$result = $collection->find( [ "Sentenca"=>["Situacao"=>['$nin'=>["nc"]]]] );

编辑: 我设法对“Situacao”应用 $nin 条件进行了查询,但由于某种原因,此查询 returns 没有任何结果。如果我尝试回显条目,我会收到错误消息:

Uncaught MongoDB\Driver\Exception\LogicException: Cursors cannot yield multiple iterators

MongoDB 论坛上有人解决了这个问题。您可以使用 '。'访问数组元素,然后使用 $nin => 'nc'.

$result = $collection->find([

    "Sentenca.0.Situacao" => [
        '$nin' => ["nc"]
    ]
    
]);