如何查找所有不是';t某一类型


How do I Find all items that aren't a certain type

在我的集合中,我需要删除任何不是特定类型的项。

$result = $db.configs.find(
            array('$not' =>  array(
                    array('$or' => array(
                            array('_id' => array('$type' => 2)),
                            array('_id' => array('$type' => 7)),
                        )
                    )
                )
            )
        );

如果我删除了元素周围的"$not"包装,它确实找到了所有类型为2和7的元素,我是否使用了not?

$and运算符与$not表达式组合使用。下面的mongoshell演示展示了如何测试应该使用的查询:

use test
db.test.insert([
    {"_id": 1, a: 1},
    {"_id": new Date(), a: 2 },
    {"_id": "foo", a: 3},
    {"_id": ObjectId("5579621f3cab061ff5c618c7"), a: 4}    
])
db.test.find({
    "$and":[ 
        { "_id": { "$not": { "$type": 7 } } },
        { "_id": { "$not": { "$type": 2 } } }
    ]
})

结果

/* 0 */
{
    "_id" : 1,
    "a" : 1
}
/* 1 */
{
    "_id" : ISODate("2015-06-24T09:59:54.230Z"),
    "a" : 2
}

等价的PHP mongo查询:

$result = $db.test.find(
    array('$and' =>  array(
            array('_id' => array(
                array("$not" => array('$type' => 2))
            ),
            array('_id' => array(
                array("$not" => array('$type' => 7))
            )
        )
    )
);