如何在猫鼬中填充另一个模型的子文档?

时间:2021-01-20 07:12:03

标签: node.js mongodb mongoose mongoose-populate

我有两个 mongodb 模型如下。

import ctypes
import PySimpleGUI as sg

def frame(sequence, key):
    radios = [[sg.Radio(text=item, group_id=key, enable_events=True,
        font='Helvetica 8', pad=(0, 0),)] for item in sequence[key]]
    column = [[sg.Column(layout=radios, size=(100, 100), scrollable=True,
        vertical_scroll_only=True, pad=(0, 0))]]
    return sg.Frame(title=key.capitalize(), layout=column)

form_items = {
    'name': ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Frank'],
    'group': ['Google', 'Apple', 'Facebook', 'Amazon'],
    'number': ['100', '200', '300', '400', '500', '600', '700', '800', '900']
    }

ctypes.windll.user32.SetProcessDPIAware()   # Set unit of GUI to pixels
sg.theme('DarkBlue')

layout = [[frame(form_items, key) for key in ['name', 'group', 'number']]]
window = sg.Window('Sandbox', layout)

while True:
    event, values = window.read()
    if event in (None, 'Exit'):
        break
window.close()

budget 字段表示 CompanySchema 中的预算字段之一。 所以我想在获取 Calc 数据时填充。 但我不知道如何填充嵌入式文档。

我尝试将 ref 值设置为 const CompanySchema = new Schema( { sections: [{ name: { type: String }, budgets: [{ // indicates from CalcSchema index: { type: Number }, title: { type: String }, values: [Number], sum: { type: Number, default: 0 }, }], }] }, { timestamps: true } ); const CalcSchema = new Schema({ budget: { type: Schema.Types.ObjectId, // I want to populate this field. this indicates budget document in Company model ref: "Company.sections.budgets" //it's possible in mongoose? }, expense: { type: Number, default: 0 } }); 。但它不起作用。

请任何人帮忙。

1 个答案:

答案 0 :(得分:1)

最后,我自己找到了答案。

有一个有用的插件。

https://github.com/QuantumGlitch/mongoose-sub-references-populate#readme

然后我了解到我的架构结构是错误的。这是mongodb中的反模式。