【发布时间】:2020-09-04 14:36:01
【问题描述】:
有没有办法导出一个集合,其中所有 DBRef 对象都嵌入在一个 JSON 中?
我尝试从 compass 导出集合,但它没有将 dbrefs 解析为嵌入的 json。
数据结构示例:
流程:
{
"_id": {
"$oid": "5f44e89aa1f4c77447629a29"
},
"name": "test process",
"description": "A test configuration for a process",
"milestones": [{
"$ref": "milestone",
"$id": {
"$oid": "5f5753636b75033a49b7cc7b"
}
}, {
"$ref": "milestone",
"$id": {
"$oid": "5f5753636b75033a49b7cc82"
}
}]
}
里程碑:
[{
"_id": {
"$oid": "5f5753636b75033a49b7cc7b"
},
"name": "S1",
"order": 0,
"results": [
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc79"
}
},
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc7a"
}
}
]
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc82"
},
"name": "S2",
"order": 1,
"results": [
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc7d"
}
},
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc7e"
}
},
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc7f"
}
},
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc80"
}
},
{
"$ref": "resultDefinition",
"$id": {
"$oid": "5f5753636b75033a49b7cc81"
}
}
]
}]
阶段:
{
"_id": {
"$oid": "5f5753636b75033a49b7cc7c"
},
"name": "P1",
"activityStream": {
"$ref": "activityStream",
"$id": {
"$oid": "5f5755076b75033a49b7cc83"
}
}
}
结果定义:
[{
"_id": {
"$oid": "5f5753636b75033a49b7cc79"
},
"name": "userresult",
"resultType": "User"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc7a"
},
"name": "planDateResult",
"resultType": "PlanDate"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc7d"
},
"name": "fileResult",
"resultType": "File"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc7e"
},
"name": "dateResult",
"resultType": "Date"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc7f"
},
"name": "textresult",
"resultType": "Text"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc80"
},
"name": "booleanResult",
"resultType": "Boolean"
},{
"_id": {
"$oid": "5f5753636b75033a49b7cc81"
},
"name": "numberResult",
"resultType": "Number"
}]
关系:
流程可以有 n 个里程碑。里程碑有一个或没有阶段。 Phase 有更多的嵌套路径,但与示例无关。里程碑可以有 n 个 ResultDefinitions
【问题讨论】:
-
您可以使用查找和$out 进行聚合查询,所有结果都生成一个集合,然后您可以导出该集合。
-
这会递归工作吗?假设我有嵌套引用 foo -> bar --> foobar 从 foo 到 bar 的关系是 1:n
-
是的,这将起作用,嵌套引用也将起作用,但取决于数据,您可以在单个查询中使用引用 ID 查找集合,只需测试它花费了多少时间。
-
好的,你能提供所有集合或文档结构的架构吗,所以我会测试和更新你。
-
表示字段和关系的示例。或所有集合的 json 文档示例,为所有集合仅添加单个文档,以便我可以了解彼此的关系字段,并进行单个查询。
标签: mongodb spring-data-mongodb mongodb-compass