$not

$not对指定的<operator-expression>执行逻辑NOT运算,并选择与<operator-expression>不匹配的文档。这包括不包含field的文档。

考虑以下查询:

db.inventory.find( { price: { $not: { $gt: 1.99 } } } )

此查询将选择inventory集合中的所有文档,其中:

{ $not: { $gt: 1.99 } }$lte运算符不同。 { $lte: 1.99 }仅返回price字段存在且其值小于或等于1.99的文档。

请记住,$not运算符仅影响其他运算符,并且不能独立检查字段和文档。因此,使用$not运算符进行逻辑析取,并使用$ne运算符直接测试字段的内容。

使用$not运算符时,请考虑以下行为:

考虑以下使用模式匹配表达式//的示例:

db.inventory.find( { item: { $not: /^p.*/ } } )

该查询将选择inventory集合中的所有文档,其中item字段的值不是*以字母p开头。

如果您使用的是 Python,则可以使用 PyMongo 驱动程序和 Python 的python:re.compile()方法编写上述查询,以编译正则表达式,如下所示:

import re
for noMatch in db.inventory.find( { "item": { "$not": re.compile("^p.*") } } ):
    print noMatch
首页