sql >> Base de Datos >  >> NoSQL >> MongoDB

Spring Data mongo para insertar valores nulos en DB

Resolví este problema usando el siguiente código

final Document document = new Document();
document.put("test1", "test1");
document.put("test2", null);
document.put("test3", "test3");
mongoTemplate.getCollection("your-collection-name").insert(document);

Aquí, en lugar de usar BSONObject, usé el objeto Document y funcionó bien.

Documento insertado en DB

{
    "_id" : ObjectId("some-id"),
    "test1" : "test1",
    "test2" : null,
    "test3" : "test3"
}