Spring Data MongoDB - 忽略空对象 [英] Spring Data MongoDB - ignore empty objects
问题描述
我正在使用 Spring Data 和 MongoDB 来保存一些文档.保存文档时,我希望 Mongo 不包含空对象.(如何)这可以实现?
I'm using Spring Data with a MongoDB to save some documents. When saving documents, I would like that Mongo does not contain empty objects. (How) can this be achieved?
假设我有以下主类:
@Document(collection = "main_doc")
public class MainDoc {
@Id
private String id;
private String title;
private SubDoc subDoc;
}
包含以下类的对象:
public class SubDoc {
private String title;
private String info;
}
现在,如果我尝试保存以下对象:
Now if I would try to save the following object:
MainDoc main = new MainDoc();
main.setTitle("someTitle");
main.setSubDoc(new SubDoc());
注意:实际上我无法控制 SubDoc 是这样设置的.它可以是空的,也可以是填充的.我想要的是,如果元素的属性/字段都为 NULL,则它根本不会存储在 mongo 中.这会在 mongo 中产生类似的结果:
Note: in reality I do not control the fact that the SubDoc is set like this. It can either be empty or filled in. What I want is that if an element's properties/fields are all NULL, it will not be stored in mongo at all. This results in something like this in mongo:
{
"_id" : "5a328f9a-6118-403b-a3a0-a55ce52099f3",
"title": "someTitle",
"subDoc": {}
}
我想要的是,如果一个元素只包含空属性,它们根本不会被保存,所以对于上面的例子,我想要以下结果:
What I would like is that if an element contains only null properties, they aren't saved at all, so for the above example I would want the following result:
{
"_id" : "5a328f9a-6118-403b-a3a0-a55ce52099f3",
"title": "someTitle"
}
文档的保存是在存储库的帮助下完成的,如下所示:
Saving of documents is done with the help of a repository as following:
@NoRepositoryBean
public interface MainRepo extends CrudRepository<MainDoc, String> {
// save inherited
}
提前致谢.
推荐答案
您可以在这里做的一件事是为 MainDoc
编写自定义转换器:
One thing you can do here is to write your custom converter for MainDoc
:
public class MainDocConverter implements Converter<MainDoc, DBObject> {
@Override
public DBObject convert(final MainDoc source) {
final BasicDbObject dbObject = new BasicDBObject();
...
if(/* validate is subdoc is not null and not empty */) {
dbOject.put("subDoc", source.getSubDoc());
}
}
}
你可以在@Configuration
文件中注册,例如:
You can register it in @Configuration
file for example:
@Configuration
@EnableMongoRepositories(basePackages = {"package"})
public class MongoConfig {
private final MongoDbFactory mongoDbFactory;
public MongoConfig(final MongoDbFactory mongoDbFactory) {
this.mongoDbFactory = mongoDbFactory;
}
@Bean
public MongoTemplate mongoTemplate() throws Exception {
final MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, getDefaultMongoConverter());
return mongoTemplate;
}
@Bean
public MappingMongoConverter getDefaultMongoConverter() throws Exception {
final MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());
converter.setCustomConversions(new CustomConversions(Arrays.asList(new MainDocConverter())));
return converter;
}
}
如果您不想为对象玩具编写自定义转换器,可以使用默认转换器并稍作修改.
If you don't want to write a custom converter for your object toy can use default one and and modify it a bit.
final Document document = (Document) getDefaultMongoConverter().convertToMongoType(mainDoc);
if(/* validate is null or is empty */) {
document .remove("subDoc");
}
mongoTemplate().save(document);
实际上这不是最好的方法.由于人们编写的空对象应存储为 {}
,但转换器可以帮助您解决问题.
Actually it's not the best way. As guys wrote empty object should be stored as {}
, but converter can help you with your case.
这篇关于Spring Data MongoDB - 忽略空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!