[MongoDB] How do I query for fields that have null values?

Source: Internet
Author: User

{Column: NULL}: The result is that the column value is null or the record does not contain the column field data;

{Column: {$ type: 10 }}: the result is data that contains the column field and the value is null;

{Column :{$ exists: false }}: the result is data that does not contain the column field;

Fields in a document may storeNullValues, as in a notional collection,Test, With the following tables ents:

{ _ Id: 1, Canceldate: Null }{ _ Id: 2 }

Different query operators treatNullValues differently:

  • The{ Canceldate : Null }Query matches documents that either containsCanceldateField whose value isNull OrThat do not containCanceldateField:

     
    DB.Test.Find( { Canceldate: Null } )

    The query returns both statements ents:

     {  "_ id"  :   1  ,   "canceldate"  :   null  } {< /span>  "_ id"  :   2  } 
  • the { canceldate : { $ type: 10 } }< /span> query matches documents that contains the canceldate Field whose value is null only ; i. e. the value of the canceldate field is of bson type null (I. e. 10 ):

    DB.Test.Find( { Canceldate : { $ Type: 10 } } )

    The query returns only the document that containsNullValue:

     
    { "_ Id" : 1, "Canceldate" : Null }
  • The{ Canceldate : { $ Exists: False } }Query matches documents that do not contain inCanceldateField:

     
    DB.Test.Find( { Canceldate : { $ Exists: False } } )

    The query returns only the document that doesNotContainCanceldateField:

     
    { "_ Id" : 2 }

See also

 

The reference documentation for$ TypeAnd$ ExistsOperators.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.