Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-45615

Ability to add sparse index on fields in an array of documents

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Index Maintenance
    • Labels:

      When an index is created on a field within an array, each the value of the field for each array item gets indexed. There is no way to use 'sparse' or a 'partialFilterExpression' to prevent nulls from being added for every item in the array that is missing the field. This is a problem when you want the index to be unique, since only one item in the array can (across all docs) can be missing the field.

      In the example, I would like a unique index on 'logins.auth_details.username' , but because both Alice and Casper have type=google logins, this isn't possible.

      db.users.insertMany( 
       { 
       
          display_name:'Alice',
              logins: [
              { 
                  type:'local',
                  auth_details: { 
                      username:'alice',
                      password:'<hashed password here>',
                  },
              },
              { 
       
                  type:'google',
                  auth_details: { 
                      google_id:'123',
                  },
              },
          ],
      },
      { 
       
          display_name:'Bob',
          logins: [
              { 
              type:'local',
              auth_details: { 
                  username:'bob',
                  password:'<hashed password here>',
                  },
              },
          ],
      },
      { 
          display_name:'Casper',
          logins: [
               { 
                  type:'local',
                  auth_details: { 
                      username:'Casper,
                      password:'<hashed password here>',
                  },
              },
              { 
                 type:'google',
                 auth_details: { 
                     google_id:'456',
                  },
              },
          ],
      },
      

       

            Assignee:
            carl.champain@mongodb.com Carl Champain (Inactive)
            Reporter:
            jmillsaps@datatrac.com jackson millsaps
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: