让我关注Java org.bson.Document
Document innerDoc = new Document();
innerDoc.put("field.with.dot", "something");
Document doc = new Document("inner", innerDoc);
[当我将文档插入到“虚线字段'field.with.dot'对存储无效”时,我检查了文档,证明mongoDB不允许嵌套Document中的虚线字段。
我该如何解决?因为该文档是动态生成的,所以它可能在几层深度中都有点分隔的字段。注意:我不想替换“点”符号。
是否有可能将虚线字段分解为嵌套字段?
{"inner": {
"field": {
"with": {
"dot": "something"
}
}
}
}
您必须构建嵌套文档
Document innerWithDoc = new Document();
innerWithDoc.put("dot", "something");
Document innerFieldDoc = new Document();
innerFieldDoc.put("with", innerWithDoc);
Document innerDoc = new Document();
innerDoc.put("field", innerFieldDoc);
Document doc = new Document("inner", innerDoc);
或者您可以在一条指令中完成相同的操作:
Document doc = new Document("inner",
new Document("field",
new Document("with",
new Document("dot", "something")
)
)
);