【问题标题】:MongoDB filteringMongoDB过滤
【发布时间】:2014-01-05 06:42:42
【问题描述】:

我有一个数组,它是 Mongodb 的过滤器。 但它不起作用,我不明白为什么。

Array
(
[deleted] => Array
    (
        [$ne] => 1
    )

    [public] => 1
    [_id] => Array
        (
            [$nin] => Array
                (
                    [0] => MongoId Object
                        (
                            [$id] => 525becec38aa9e28201f1d68
                        )
                    [1] => MongoId Object
                        (
                            [$id] => 525becb438aa9e963a1f1d55
                        )
                    [2] => MongoId Object
                        (
                            [$id] => 525bec7438aa9e8d3a1f1d56
                        )
                    [3] => MongoId Object
                        (
                            [$id] => 525bec1438aa9e6c6d1f1d69
                        )
                    [4] => MongoId Object
                        (
                            [$id] => 525bebcf38aa9e8c3a1f1d57
                        )
                    [5] => MongoId Object
                        (
                            [$id] => 525beb3038aa9e8d3a1f1d55
                        )
                )
        )
)

我需要找到标识符不在列表中的所有对象。

【问题讨论】:

  • 那是什么编程语言?我认为它可能是 PHP,但我不确定。请添加适当的标签。
  • 我不明白你在做什么。请更具体,并向我们展示您已经尝试过的内容。 MongoDB 查询需要做什么?
  • 我正在尝试获取不包含在列表中的所有对象,数组中的列表 $nin if( !empty( $profile['answered'] ) ) $filter['_id ']['$nin'][] = $profile['已回答']; if( !empty( $profile['wrongAnswered'] ) ) $filter['_id']['$nin'] = $profile['wrongAnswered']; $object = $this->mongo->questions->find( $filter )->limit(1)->sort( array( 'gallery.0' => -1 ) );

标签: php arrays mongodb nosql


【解决方案1】:

您的查询应如下所示:

$filter = array(
    '_id' => array(
         '$nin' => array(
              new MongoId('525becec38aa9e28201f1d68'),
              //etc...
          )
     ),
     'deleted' => array(
          '$ne' => 1
      )

);

$result = $this->mongo->questions->find($filter)->sort(//however you're sorting);

不确定您为什么将结果限制为 1 个文档,除非那是您真正想要的。

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多