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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:58:52+00:00 2026-05-25T01:58:52+00:00

The mongo documentation states that When a document is saved to a collection with

  • 0

The mongo documentation states that “When a document is saved to a collection with unique indexes, any missing indexed keys will be inserted with null values. Thus, it won’t be possible to insert multiple documents missing the same indexed key.”

So is it impossible to create a unique index on an optional field? Should I create a compound index with say a userId as well to solve this? In my specific case I have a user collection that has an optional embedded oauth object.
e.g.

>db.users.ensureIndex( { "name":1, "oauthConnections.provider" : 1, "oauthConnections.providerId" : 1 } );

My sample user

{  name: "Bob"
   ,pwd: "myPwd"
   ,oauthConnections [
      {
         "provider":"Facebook",
         "providerId" : "12345",
         "key":"blah"
      }
     ,{
         "provider":"Twitter",
         "providerId" : "67890",
         "key":"foo"
      }
     ]
}
  • 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-25T01:58:53+00:00Added an answer on May 25, 2026 at 1:58 am

    I believe that this is possible: You can have an index that is sparse and unique. This way, non-existant values never make it to the index, hence they can’t be duplicate.

    Caveat: This is not possible with compound indexes. I’m not quite sure about your question. Your citing a part of the documentation that concerns compound indexes — there, missing values will be inserted, but from your question I guess you’re not looking for a solution w/ compound indexes?

    Here’s a sample:

    > db.Test.insert({"myId" : "1234", "string": "foo"});
    > show collections
    Test
    system.indexes
    >
    > db.Test.find();
    { "_id" : ObjectId("4e56e5260c191958ad9c7cb1"), "myId" : "1234", "string" : "foo" }
    >
    
    > db.Test.ensureIndex({"myId" : 1}, {sparse: true, unique: true});
    >
    > db.Test.insert({"myId" : "1234", "string": "Bla"});
    E11000 duplicate key error index: test.Test.$myId_1  dup key: { : "1234" }
    >
    > db.Test.insert({"string": "Foo"});
    > db.Test.insert({"string": "Bar"});
    > db.Test.find();
    { "_id" : ObjectId("4e56e5260c191958ad9c7cb1"), "myId" : "1234", "string" : "foo" }
    { "_id" : ObjectId("4e56e5c30c191958ad9c7cb4"), "string" : "Foo" }
    { "_id" : ObjectId("4e56e5c70c191958ad9c7cb5"), "string" : "Bar" }
    

    Also note that compound indexes can’t be sparse

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

Sidebar

Related Questions

Is mono the only route , any specific visual studio like editors that you
In Mongo my understanding is that you can have databases and collections. I'm working
Mono Documentation The documentation is missing here. What does it do?
After some searches, I found the documentation of mongo_mapper is poor. There is only
Does anyone know the correct way to explicitly specify which rules Gendarme will use?
mono creates its own debug targets called .mdb files when you use the mcs
Mono 2.0 was just officially released . What single feature do you think is
Mono claims to be compatible with .NET. Have you tried it? Can you share
Does Mono .NET support and compile C++ / CLI? If not, do you know
Has Mono been ported to the iPhone yet? I'd love to write .NET apps

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.