Java 从查询返回的文档向子文档添加字段

Java 从查询返回的文档向子文档添加字段,java,mongodb,Java,Mongodb,我想向返回的子文档添加一个字段,如下所示: { "type": "product", "code": "abc", "team": "A1", "modules": [{ "type": "module", "code": "cde" //Add a new field here }] } 我可以通过document.append()在文档中添加新字段,但不能添加到子文档中 以下是我的Java代码: BasicDBObject query

我想向返回的子文档添加一个字段,如下所示:

{
"type": "product",
"code": "abc",
"team": "A1",
"modules": [{
        "type": "module",
        "code": "cde"
        //Add a new field here
    }]
}
我可以通过
document.append()
在文档中添加新字段,但不能添加到子文档中

以下是我的Java代码:

BasicDBObject query  = new BasicDBObject("code", "abc");
AggregateIterable<Document> findIterable = collection.aggregate(Arrays.asList(
    new Document("$match", query),
    new Document("$project", new Document("_id", 0).append("type", 1)
                                               .append("code", 1)
                                               .append("team", 1)
                                               .append("modules", 1)
                                               ),
    new Document("$sort", new Document("code", 1))
));

mongoCursor = findIterable.iterator(); 

while(mongoCursor.hasNext()){ 
    Document document = mongoCursor.next();
}
BasicDBObject query=新的BasicDBObject(“代码”、“abc”);
AggregateIterable FindItemerable=collection.aggregate(Arrays.asList(
新文档(“$match”,查询),
新文档(“$project”,新文档(“\u id”,0)。追加(“type”,1)
.append(“代码”,1)
.append(“团队”,1)
.append(“模块”,1)
),
新文件(“$sort”,新文件(“代码”,1))
));
mongoCursor=FindItemerable.iterator();
while(mongoCursor.hasNext()){
Document Document=mongoCursor.next();
}

有人能帮忙吗?提前谢谢大家

-检查此项以解析,谢谢您的评论!!但是我使用的文档是这样的:是否可以将org.bson.document转换为您提到的文档?为什么需要聚合?是否只想向子文档添加新字段并更新集合?