Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
Setting a field as index only if it is not empty

I have a a structure with two keys as follows:

{
    "cannot_be_empty": "hello",
    "can_be_empty": "world"
}

Right now I set the index to be a combination of can_be_empty and cannot_be_empty.

db.collection.Indexes.CreateOne(IndexModel{{Keys: bson.D{
    {Key: "cannot_be_empty", Value: 1}, 
    {Key: "can_be_empty", Value: 1},
}})

However, I want to make it such that can_be_empty must be unique across all fields unless it is an empty string. Is there a way to do this? I think https://docs.mongodb.com/manual/core/index-partial/ might be the way to go, but I'm not too sure how it works in go.

Thanks!

UPDATE: I've found out that $ne does not work, so I tried this and presently my tests are still failing, but I'll try to find out why.

// Set partial index option
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
    Key:   "can_be_empty",
    Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
    {Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}},
    {Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)

UPDATE 2: I didn't know I had to add a unique option for this to work. In any case, I also need the compound key of can_be_empty and cannot_be_empty to be unique.

FINAL UPDATE: I've accepted the answer, but will also post my own Golang version.

myUniqueIndex := options.Index().SetUnique(true)
myPartialIndex := options.Index().SetPartialFilterExpression(bson.D{{
    Key:   "can_be_empty",
    Value: bson.D{{Key: "$gt", Value: ""}},
}})
// Set up all indexes
indexes := []mongo.IndexModel{
    {Keys: bson.D{{Key: "cannot_be_empty", Value: 1}, {Key: "can_be_empty", Value: 1}}, Options: myUniqueIndex},
    {Keys: bson.D{{Key: "can_be_empty", Value: 1}}, Options: myPartialIndex},
}
// Create index, no error
db.collection.Indexes().CreateMany(ctx, indexes)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

you can use $gt empty string.

db.collection.createIndex(
   { can_be_empty: 1, cannot_be_empty: 1 },
   { unique: true, partialFilterExpression: { "can_be_empty": {  "$gt": "" }  } }
)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda