发布于 2015-09-14 15:08:41 | 126 次阅读 | 评论: 0 | 来源: 网络整理

$not

Syntax: { field: { $not: { <operator-expression> } } }

$not performs a logical NOT operation on the specified <operator-expression> and selects the documents that do not match the <operator-expression>. This includes documents that do not contain the field.

Consider the following query:

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

This query will select all documents in the inventory collection where:

  • the price field value is less than or equal to 1.99 or
  • the price field does not exist

{ $not: { $gt: 1.99 } } is different from the $lte operator. { $lt: 1.99 } returns only the documents where price field exists and its value is less than or equal to 1.99.

Remember that the $not operator only affects other operators and cannot check fields and documents independently. So, use the $not operator for logical disjunctions and the $ne operator to test the contents of fields directly.

Consider the following behaviors when using the $not operator:

  • The operation of the $not operator is consistent with the behavior of other operators but may yield unexpected results with some data types like arrays.

  • The $not operator does not support operations with the $regex operator. Instead use // or in your driver interfaces, use your language’s regular expression capability to create regular expression objects.

    Consider the following example which uses the pattern match expression //:

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

    The query will select all documents in the inventory collection where the item field value does not start with the letter p.

    If using PyMongo’s re.compile(), you can write the above query as:

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

也可以参考

find(), update(), $set, $gt, $regex, PyMongo, driver.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务