Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:28:41+00:00 2026-06-11T09:28:41+00:00

It seems to me that when you are creating a Mongo document and have

  • 0

It seems to me that when you are creating a Mongo document and have a field {key: value} which is sometimes not going to have a value, you have two options:

  1. Write {key: null} i.e. write null value in the field
  2. Don’t store the key in that document at all

Both options are easily queryable, in one you query for {key : null} and the other you query for {key : {$exists : false}}.

I can’t really think of any differences between the two options that would have any impact in an application scenario (except that option 2 has slightly less storage).

Can anyone tell me if there are any reasons one would prefer either of the two approaches over the other, and why?

EDIT

After asking the question it also occurred to me that indexes may behave differently in the two cases i.e. a sparse index can be created for option 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:28:43+00:00Added an answer on June 11, 2026 at 9:28 am

    Indeed you have also a third possibility :
    key: "" (empty value)

    And you forget a specificity about null value.
    Query on
    key: null will retrieve you all document where key is null or where key doesn’t exist.

    When a query on $exists:false will retrieve only doc where field key doesn’t exist.

    To go back to your exact question it depends of you queries and what data represent.
    If you need to keep that, by example, a user set a value then unset it, you should keep the field as null or empty. If you dont need, you may remove this field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that the java code generator framework CodeModel is not capable of creating
Seems simple enough. I'm creating a search engine that returns results to the user
It seems that it is not advisable to use <meta http-equiv=REFRESH CONTENT=3;url=url> for redirects
I'm using addTimeInterval for creating local notification but it seems that it is now
It seems that creating a pool and .recyling the system when I am done
I am creating a Map function in MongoDB that have a problem as soon
I need to use a SoapExtension subclass (which I have created), but it seems
In school I have learned that creating UML Diagrams/Models is great design practice before
After creating a blank xib file, it seems that we can either drag a
Ok, so Less.js has come along, and it seems that my dreams of creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.