为树视图填充mongoose co-related模式

时间:2017-10-27 15:56:44

标签: node.js mongodb bluebird

我创建了以下mongoose架构

工作空间model.js

var mongoose=require('mongoose')
var uniqueValidator=require('mongoose-unique-validator');
var workspaceSchema=new mongoose.Schema({
  name:{type: String, lowercase: true, required: [true, "can't be blank"],
  //match: [/^[a-zA-Z0-9]$/, 'is invalid'],
  index: true,
  unique: true
},
  processes:[{type:mongoose.Schema.Types.ObjectId,ref:'Process'}],

  },{timestamp:true})

workspaceSchema.plugin(uniqueValidator,{message:'is already taken'});

module.exports=mongoose.model('Workspace',workspaceSchema)

过程model.js

var mongoose=require('mongoose')
var uniqueValidator=require('mongoose-unique-validator')
var processSchema=new mongoose.Schema({
  name:{type: String, lowercase: true, required: [true, "can't be blank"],
  //match: [/^[a-zA-Z0-9]$/, 'is invalid'],
  index: true,
  unique: true
},
  state:String, //enabled/disabled/started/stopped/checkin/checkout/starting/stopping
  queues:[{type:mongoose.Schema.Types.ObjectId,ref:'Queue'}],
  workspace:{type:mongoose.Schema.Types.ObjectId,ref:'Workspace'}
},{timestamp:true})

processSchema.plugin(uniqueValidator,{message:'is already taken'});

module.exports=mongoose.model('Process',processSchema)

队列model.js

var mongoose=require('mongoose')
var uniqueValidator=require('mongoose-unique-validator');
var queueSchema=new mongoose.Schema({
  name:{type: String, lowercase: true, required: [true, "can't be blank"],
  //match: [/^[a-zA-Z0-9]$/, 'is invalid'],
  index: true,
  unique: true
},
  type:String, //start//end/custom/decision/split/join
  prev:{type:mongoose.Schema.Types.ObjectId,ref:'Queue'},
  next:{type:mongoose.Schema.Types.ObjectId,ref:'Queue'},
  workitems:[{type:mongoose.Schema.Types.ObjectId,ref:'Workitem'}],
  process:{type:mongoose.Schema.Types.ObjectId,ref:'Process'}
},{timestamp:true})
queueSchema.plugin(uniqueValidator,{message:'is already taken'});

module.exports=mongoose.model('Queue',queueSchema)

工作项-model.js

var mongoose=require('mongoose')
var uniqueValidator=require('mongoose-unique-validator');
var workitemSchema=new mongoose.Schema({
  status:String, //locked//processing//done//available
  lockedby:String,
  queue:{type:mongoose.Schema.Types.ObjectId,ref:'Queue'},
  process:{type:mongoose.Schema.Types.ObjectId,ref:'Process'}
},{timestamp:true})
//workitemSchema.plugin(uniqueValidator,{message:'is already taken'});

module.exports=mongoose.model('Workitem',workitemSchema)

喜欢以下面的树视图格式填充数据

工作空间 - > [进程] - > [排队] - > [工作项目]

尝试了以下但是所有子路径和数组项都没有填充。

...

var Process=require('./models/process-model')
var Workspace=require('./models/workspace-model')
var Workitem=require('./models/workitem-model')
var Queue=require('./models/queue-model')


Workspace.findOne({name:'dummy'}).populate({
    path: 'processes',
    // Get friends of friends - populate the 'friends' array for every friend
    populate: { path: 'queues' }
  }).exec().then((data)=>{console.log(data})
  
  

以下是样本数据文件..

workspace

{
  "_id": ObjectId("59f313665087dc2648ab2dc1"),
  "name": "dummy",
  "processes": [
    ObjectId("59f313665087dc2648ab2dc2")
  ],
  "__v": 1
}

process {
  "_id": ObjectId("59f313665087dc2648ab2dc2"),
  "workspace": ObjectId("59f313665087dc2648ab2dc1"),
  "state": "enabled",
  "name": "outward",
  "queues": [
    ObjectId("59f3312ee9bbaa27e032f55b"),
    ObjectId("59f3312ee9bbaa27e032f55c"),
    ObjectId("59f3312ee9bbaa27e032f55d")
  ],
  "__v": 1
}
queue {
  "_id": ObjectId("59f3312ee9bbaa27e032f55c"),
  "process": ObjectId("59f313665087dc2648ab2dc2"),
  "type": "custom",
  "name": "data entry",
  "workitems": [],
  "__v": 0,
  "next": ObjectId("59f3312ee9bbaa27e032f55d"),
  "prev": ObjectId("59f3312ee9bbaa27e032f55b")
} {
  "_id": ObjectId("59f3312ee9bbaa27e032f55d"),
  "process": ObjectId("59f313665087dc2648ab2dc2"),
  "type": "end",
  "name": "work exit",
  "workitems": [],
  "__v": 0,
  "prev": ObjectId("59f3312ee9bbaa27e032f55c")
} {
  "_id": ObjectId("59f3312ee9bbaa27e032f55b"),
  "process": ObjectId("59f313665087dc2648ab2dc2"),
  "type": "start",
  "name": "work introduction",
  "workitems": [
    ObjectId("59f33b3b99506b25f033ecf5")
  ],
  "__v": 1,
  "next": ObjectId("59f3312ee9bbaa27e032f55c")
}
workitem {
  "_id": ObjectId("59f33b3b99506b25f033ecf5"),
  "status": "available",
  "__v": 0,
  "process": ObjectId("59f313665087dc2648ab2dc2"),
  "queue": ObjectId("59f3312ee9bbaa27e032f55b")
}

请帮忙。 提前谢谢..

1 个答案:

答案 0 :(得分:0)

问题是console.log在2-3级嵌套级别后无法打印。 解决方案如下

var util = require('util')

  .....
//following line will print nested objects
console.log(util.inspect(data, false, null))
  ....