如何在猫鼬中分组以填充内部字段

时间:2020-03-04 15:45:19

标签: javascript node.js mongodb mongoose

我试图为此寻求高低求索,但对于如何做到这一点并没有运气。

我需要通过作为子字段的子字段的字段分组依据。这是我的模式

const BoxSchema = new mongoos.Schema({
    name: {
    type: String,
    required: true,
  },

   // List of workflows inside one Box
   workflows: [{
    type: ObjectId,
    ref: 'Workflow',
  }],
})

const WorkflowSchema = new mongoose.Schema({
   name: {
    type: String,
    required: true,
   }, 

    // The box the workflow belongs to
    boxId: {
    type: ObjectId,
    required: true,
    ref: 'Box',
   },

    // List of cards inside the workflow
    cards: {
    type: [ObjectId],
    ref: 'Card',
  },

    // List of groups inside the workflow
    groups: {
    type: [ObjectId],
    ref: 'Group',
  },
})

const GroupSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },

  // The box the group belongs to
  boxId: {
    type: ObjectId,
    required: true,
    ref: 'Box',
  },

  // The workflow the group belongs to
  workflow: {
    type: ObjectId,
    required: true,
    ref: 'Workflow',
  },
})

const CardSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },

  // The Workflow the card belongs to
  workflow: {
    type: ObjectId,
    ref: 'Workflow',
  },

  // The Group the card belongs to
  group: {
    type: ObjectId,
    ref: 'Group',
  },

  properties: [{
    name: {
      type: String,
      required: true,
    },
    items: [{
      value: { type: String, default: '' },
      isSelected: { type: Boolean, default: false },
    }],
  }],

  isTemplate: {
    type: Boolean,
  },
})

我想获取每个卡中Workflows字段Box分组的Group 分组中的所有workflows: [ { "id": "5e39384c7921940fa8b5500a", "name": "Input", "group": [ "cards": [...] ], "group": [ "cards": [...] ], "group": [ "cards": [...] ], } ] 的列表。也许是这样的东西?

Group

我用$ lookup或$ match尝试了很多,我只得到空数组,所以我终于在这里求助。不胜感激。

P.S。我知道可以用不同的方式来制作架构,但是问题是Group架构是新添加的,这就是为什么我决定在每张卡中标记它的原因,因为该卡可能没有{{1} },但可以是Workflow

的一部分

谢谢。

编辑:

每个集合的样本数据:

BOX

{
    "_id" : ObjectId("5e3934047921940fa8b54ffd"),
    "cards" : [
        ObjectId("5e4ff643a966a14d44e26485"), 
        ObjectId("5e4ff647a966a14d44e26494"), 
        ObjectId("5e4ff64ba966a14d44e264a3"), 
        ObjectId("5e4ff653a966a14d44e264ae"), 
    ],
    "workflows" : [ 
        ObjectId("5e39384c7921940fa8b5500a"), 
        ObjectId("5e39385f7921940fa8b5500c"), 
        ObjectId("5e39386a7921940fa8b5500e"), 
    ],
    "name" : "My Workplace",
    "createdAt" : ISODate("2020-02-04T09:06:12.686Z"),
    "updatedAt" : ISODate("2020-03-02T15:22:21.563Z"),
    "__v" : 0
}

工作流程

{
    "_id" : ObjectId("5e39384c7921940fa8b5500a"),
    "cards" : [ 
        ObjectId("5e553667ed55fa44104d860b"), 
        ObjectId("5e5777e4ca0d275bc8cb880a"), 
        ObjectId("5e53c6a7bf63a0169c7fbdb6"), 
        ObjectId("5e393c7b7921940fa8b550d5"), 
    ],
    "name" : "Input",
    "boxId" : ObjectId("5e3934047921940fa8b54ffd"),
    "createdAt" : ISODate("2020-02-04T09:24:28.753Z"),
    "updatedAt" : ISODate("2020-03-04T11:04:47.716Z"),
    "__v" : 0
}

我没有任何分组信息,因为它是一个新架构

{
    "_id" : ObjectId("5e3938c37921940fa8b55012"),
    "title" : "Main Article",
    "isTemplate" : true,
    "box" : ObjectId("5e3934047921940fa8b54ffd"),
    "properties" : [ 
        {
            "_id" : ObjectId("5e3939a57921940fa8b55031"),
            "name" : "Größe",
            "items" : [ 
                {
                    "_id" : ObjectId("5e3939c67921940fa8b55035"),
                    "value" : "Large - 1000 Wörter",
                    "isSelected" : true
                }, 
                {
                    "_id" : ObjectId("5e3939d27921940fa8b55036"),
                    "value" : "Medium - 650 Wörter",
                    "isSelected" : false
                }, 
                {
                    "_id" : ObjectId("5e3939da7921940fa8b55037"),
                    "value" : "Small - 250 Wörter",
                    "isSelected" : false
                }
            ]
        }
    ],
    "createdAt" : ISODate("2020-02-04T09:26:27.489Z"),
    "updatedAt" : ISODate("2020-02-04T09:31:40.182Z"),
    "__v" : 0
}

0 个答案:

没有答案