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

98
Vistas
Mongodb java driver query with binary _id

I have a few entries in a mongodb database. They have binary _ids. If I query with a mongo client like robomongo I'm able to find the entry I'm looking for:

db.getCollection('comment').find({"_id" : new BinData(0,"nCgNlWhzJM9/lHDVQmXQrg==")})

However, I'm having serious issues with doing the same in java. Here is what I'm trying to do:

final MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("comment");

mongoCollection
             .find(eq("_id", new Binary((byte) 0, "nCgNlWhzJM9/lHDVQmXQrg==".getBytes(StandardCharsets.UTF_8))))
             .first()

Sadly this is not working. Trying to find an item with a string type works (like eq("author", "someone) ).

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your _id value nCgNlWhzJM9/lHDVQmXQrg== is shown Base64 encoded, so your java query should be written:

    Document doc = mongoCollection
    .find(eq("_id", new Binary((byte) 0, Base64.getDecoder().decode("nCgNlWhzJM9/lHDVQmXQrg=="))))
    .first();

To decode the key before using it to create the binary filter.

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