所以我有一个 POJO 对象,我正在使用 Jongo 创建并保存到 MongoDB 集合中:
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;
}
}
将对象保存到 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);
}
}
此测试可以很好地保存对象:
{
"_id" : ObjectId("543cf1a6cd8936deafcf66cf"),
"name" : "objectA",
"mappings" : {}
}
{
"_id" : ObjectId("543cf1a6cd8936deafcf66d0"),
"name" : "objectB"
}
但我真正想要的是 objectB 中的空映射显示为
"mappings" : null
我知道集合中的字段可以有一个空值,但我不知道如何使用 jongo 驱动程序执行此操作,有什么想法吗?
仅供参考,我使用的是 jongo V1.1