首页 > 解决方案 > 未填充 2 级对象中的 mongoose ref

问题描述

我有两个模式WorkflowCloudFunction其中工作流模式包含节点数组,它是对象数组。每个对象都有nodeConfig包含cloudFunction字段的对象。 cloudFunction字段是对CloudFunction模型的引用。

这是一个架构:

工作流程:

 {
  name: {
    type: String,
    required: [true, "Function name is required"]
  },
  startNode:{
    type:{}
  },
  nodes: {
    type: [{ 
        nodeType:{type:String},
        nodeId:{type:String},
        nodeName:{type:String},
        nodeConfig:{
          
          type:{
            headerTemplate:{type:[], default:[]},
            cloudFunction:{type:Schema.Types.ObjectId, ref:"CloudFunction"}, 
        },
      },
      
      }]
  },

 }

云功能

{
  functionName: {
    type: String,
    required: [true, "Function name is required"]
  },
  project:{
    type:String,
  }
}

我尝试了 SO questions 中提到的多种方法,但似乎对我的模型没有任何作用。任何线索模型或下面提到的查询有什么问题:

model.find({"name":req.query.workflowName})
.populate({path:'nodes.nodeConfig.cloudFunction',model:'CloudFunction'})

此查询返回数据但不填充 CloudFunction Model 的projectandfunctionName字段。

在 mongoDB 集合名称中显示为:

以下是数据在 mongodb 中的外观:

工作流数据

{

"_id" : ObjectId("60a3fba5dc21904274910be7"),
"name" : "dfs",
"nodes" : [
    {
        "nodeConfig" : {
            "headerTemplate" : [ ],
            "cloudFunction" : ObjectId("60b22fb40523ea419c1253af"),
        },
        "_id" : ObjectId("60bf7d1e09497a499862ce78"),
        "nodeType" : "TEST",
        "nodeId" : "1622471592835",
        "nodeName" : "test"
    }
],
"__v" : 0,
}

云功能数据

{
"_id" : ObjectId("60b22fb40523ea419c1253af"),
"functionName" : "test",
"project":"TEST_PROJ"
"__v" : 0,

}

标签: mongodbmongoose

解决方案


推荐阅读