我尝试使mongodb视图(未实现)聚合来自嵌套对象的数据。
简化数据结构:
@Document(collection= "ObjectA")
class ObjectA {
String id;
String propA;
List<ObjectB> list;
}
class ObjectB {
String name;
}
我创建了一个名为ObjectBView
的视图,并首先将一个字段添加到了画中线。不幸的是,即使插入新的ObjectA
之后,该视图也不包含数据。
集合ObjectBView
存在于日志和mongo-connector中。在GUI中单击“查看”按钮后,它会返回错误提示:Collection not found!
这是我的代码:
public void initMongoView() {
if (!mongoTemplate.collectionExists("ObjectBView")) {
MongoDatabase mongoDatabase = this.mongoClient.getDatabase(dbName);
mongoDatabase.createView("ObjectBView","ObjectA",asList(buildViewProperties()));
log.info(mongoTemplate.getcollectionNames().toString()); // <- view exists!
}
}
private Document buildViewProperties() {
Document pipeline = new Document("$project",new Document("propA",1));
return pipeline;
}
项目使用mongo-java-driver-3.11.2
。
为什么视图不包含数据?