-
Type:
New Feature
-
Resolution: Done
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Index Maintenance
-
(copied to CRM)
-
None
-
None
-
None
-
None
-
None
-
None
-
None
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',
},
},
],
},