DEV Community

aristides villarreal
aristides villarreal

Posted on • Edited on

Colecciones dinámicas con jmoordbcore y MongoDB solo para embebido

A partir de Jmoordbcore 1.3.0 es posible generar de manera dinámica colecciones en MongoDB.

No aplica para entidades que utilizan referencias.

Sin especificar el nombre de base de datos o colección, se toma la información del archivo microprofile-config.properties.


#-- Database
mongodb.database=accreditation
mongodb.database1=sft
mongodb.database2=practicadb

Enter fullscreen mode Exit fullscreen mode

En el controller se realiza.


@Path("icono")
@RolesAllowed({"admin"})
public class IconoController {

    @Inject
    IconoRepository iconoRepository;


 @POST

 @RolesAllowed({"admin"})
 public Response save(
            @RequestBody(description = "Crea un nuevo icono.", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Icono.class))) Icono icono) {
   Optional<Icono> iconoOptional = iconoRepository.save(icono);
        if (iconoOptional.isPresent()) {
            saveHistory(icono);
            return Response.status(201).entity(iconoOptional.get()).build();
        } else {
            return Response.status(400).entity("Error " + iconoRepository.getJmoordbException().getLocalizedMessage()).build();
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Agregando un nombre de base de datos y colección

Solo tiene que utilizar el método setDynamicCollection().


@Path("icono")
@RolesAllowed({"admin"})
public class IconoController {

   @Inject
   IconoRepository iconoRepository;


 @POST

 @RolesAllowed({"admin"})
 public Response save(
            @RequestBody(description = "Crea un nuevo icono.", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Icono.class))) Icono icono) {
    iconoRepository.setDynamicDatabase("database2");
    iconoRepository.setDynamicCollection("icono_2");
    Optional<Icono> iconoOptional = iconoRepository.save(icono);
        if (iconoOptional.isPresent()) {
            saveHistory(icono);
            return Response.status(201).entity(iconoOptional.get()).build();
        } else {
            return Response.status(400).entity("Error " + iconoRepository.getJmoordbException().getLocalizedMessage()).build();
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

AWS GenAI Live!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️