【问题标题】:Doctrine and Mongodb: Remove all documents with field not in arrayDoctrine 和 Mongodb:删除所有字段不在数组中的文档
【发布时间】:2016-06-21 15:39:39
【问题描述】:

我想删除所有具有不在我拥有的 id 数组中的字段的文档。当我尝试时,它只会从我的数据库中删除所有内容。

这是我的代码:

public function removeNotInListingIdList($ids, $country) {
      $dm = $this->getDocumentManager();
      $dm->createQueryBuilder('DnDReactivePandoraBundle:Property')
        ->remove()
        ->field('listingId')->notIn($ids)
        ->field('country', $country)
        ->getQuery()
        ->execute();
  }

我在这里做错了什么?

更新

这是debug指令的输出:

Array
(
    [type] => 3
    [query] => Array
        (
            [listingId] => Array
                (
                    [$nin] => Array
                        (
                            [0] => 15-77
                            [1] => 15-79
                            [2] => 15-82
                            [3] => 13-39
                            [4] => 15-85
                            [5] => 15-86
                            [6] => 15-60
                            [7] => 15-61
                            [8] => 16-8
                        )

                )

            [country] => Mexico
        )

    [newObj] => Array
        (
        )

)

【问题讨论】:

  • ->field('country', $country) 无效,您应该使用->field('country')->equals($country)。此外,如果这不是唯一的问题,请将 ->execute()->debug() 交换并将结果粘贴到此处(查询不会执行,但您会得到将传递给驱动程序的查询)
  • 感谢@malarzm 我刚刚将执行换成调试并粘贴了结果。
  • 嗯,在您的数据库中给定 listingIds 是正常值,它应该可以正常工作...您是否尝试在 mongo 的 shell 中自行将查询放在一起?
  • 我现在正在尝试
  • @malarzm 我认为这可能与我没有使用 mongo $and 运算符有关

标签: php mongodb symfony doctrine-orm


【解决方案1】:

您所在的国家/地区未正确构建。应该是:

   $dm = $this->getDocumentManager();
   $dm->createQueryBuilder('DnDReactivePandoraBundle:Property')
      ->remove()
      ->field('listingId')->notIn($ids)
      ->field('country')->equals($country)
      ->getQuery()
      ->execute();

【讨论】:

    猜你喜欢
    • 2013-08-26
    • 2015-01-16
    • 2021-04-14
    • 2012-05-13
    • 2020-11-21
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多