1

so I have a POJO object that I am creating and saving to a MongoDB collection using Jongo:

import java.util.Map;

public class MyObject {

    private String name;
    private Map<String, String> mappings;

    public MyObject() {

    }

    public MyObject(String name, Map mappings) {
        this.name = name;
        this.mappings = mappings;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, String> getMappings() {
        return mappings;
    }

    public void setMappings(Map<String, String> mappings) {
        this.mappings = mappings;
    }
}

Test class for saving objects to mongo:

import com.mongodb.DB;
import com.mongodb.MongoClient;
import org.jongo.Jongo;
import org.jongo.MongoCollection;

import java.net.UnknownHostException;
import java.util.HashMap;

public class NullFieldTest {

    public static void main(String[] args) throws UnknownHostException {

        DB db = new MongoClient("localhost", 27017).getDB("testDB") ;
        Jongo jongo = new Jongo(db);
        MongoCollection testCollection = jongo.getCollection("testCollection");

        MyObject objectA = new MyObject("objectA", new HashMap());
        MyObject objectB = new MyObject("objectB", null);

        testCollection.save(objectA);
        testCollection.save(objectB);

    }
}

This test saves the objects fine:

{
    "_id" : ObjectId("543cf1a6cd8936deafcf66cf"),
    "name" : "objectA",
    "mappings" : {}
}
{
    "_id" : ObjectId("543cf1a6cd8936deafcf66d0"),
    "name" : "objectB"
}

but what I really want is for the null mappings in objectB to appear as

"mappings" : null

I know that a field within a collection can have a null value, but I dont know how to do this with the jongo driver, any ideas?

FYI, I'm using jongo V1.1

Garlando
  • 196
  • 7

2 Answers2

3

problem solved, my issue was in the serialization of the objects, added an annotation from Jackson to include even null fields:

@JsonInclude(JsonInclude.Include.ALWAYS)
public class MyObject {

Found more details on this post: How to tell Jackson to ignore a field during serialization if its value is null?

Community
  • 1
  • 1
Garlando
  • 196
  • 7
0

Jongo comes with a pre-configured Jackson which ignore null fields : https://github.com/bguerout/jongo/blob/443b461e47acdcffb9f51efafb291b7e0c805c26/src/main/java/org/jongo/marshall/jackson/configuration/VisibilityModifier.java

You can change this configuration by creating a custom Mapper using JacksonMapper.Builder :

Mapper mapper = new JacksonMapper.Builder().addModifier(new MapperModifier() {
    public void modify(ObjectMapper mapper) {
        mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    }
}).build();
Jongo jongo = new Jongo(db, mapper);
Benoît Guérout
  • 1,977
  • 3
  • 21
  • 30
  • Thanks, I've gone with explicitly adding the annotation where I need it, to ensure any mappers made in future use it without having to be customised – Garlando Oct 16 '14 at 12:51