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 7657181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:57:37+00:00 2026-05-31T12:57:37+00:00

I was wondering if there is way to force a unique collection entry but

  • 0

I was wondering if there is way to force a unique collection entry but only if entry is not null.
e
Sample schema:

var UsersSchema = new Schema({
    name  : {type: String, trim: true, index: true, required: true},
    email : {type: String, trim: true, index: true, unique: true}
});

’email’ in this case is not required but if ’email’ is saved I want to make sure that this entry is unique (on a database level).

Empty entries seem to get the value ‘null’ so every entry wih no email crashes with the ‘unique’ option (if there is a different user with no email).

Right now I’m solving it on an application level, but would love to save that db query.

thx

  • 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-05-31T12:57:39+00:00Added an answer on May 31, 2026 at 12:57 pm

    As of MongoDB v1.8+ you can get the desired behavior of ensuring unique values but allowing multiple docs without the field by setting the sparse option to true when defining the index. As in:

    email : {type: String, trim: true, index: true, unique: true, sparse: true}
    

    Or in the shell:

    db.users.ensureIndex({email: 1}, {unique: true, sparse: true});
    

    Note that a unique, sparse index still does not allow multiple docs with an email field with a value of null, only multiple docs without an email field.

    See http://docs.mongodb.org/manual/core/index-sparse/

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

Sidebar

Related Questions

This is probably not possible, but I'm wondering if there's a way to make
I'm wondering if is there a way to force MSSQL Management Studio to produce
Just wondering if there's some way to force the browser to bring up the
I was wondering is there a way to prevent ' git push --force '
all, I was just wondering if there was a logistical way to not reassign
I'm wondering if there is a way to force TeamCity to run a given
I was wondering if there's a way to force the window created using glutCreateWindow
I was wondering if there is a way to force phpDocumentor to print out
I'm wondering if there is a way to force Heroku to recompile the slug
I'm wondering if there's a way to force Highcharts to show ALL of the

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.