【问题标题】:Mongodb: how to populate a schema referenced in my schema through my apiMongodb:如何通过我的 api 填充我的架构中引用的架构
【发布时间】:2019-08-18 18:13:30
【问题描述】:

我想编辑我的 Api 以便能够填充引用的架构。 这是我的架构:

export const taskSchema = new Schema ({
    user:{
        type: String,
        required: true
    },
    project: { 
        type: String,
        required: true
    },
    issue: {
        type: String,
        required: true
    },
    title: {
        type: String,
        required: true
    },
    records : [{
   
        _domain: {
            type: Schema.Types.ObjectId,
            ref: 'TaskDomains'
        },
        time: {
            type:Number
        }
    
    }],
    
   
    dateCreated: {
     
        type: Date,
        default: Date.now
    }
});

我的 taskDomain 架构:

export const TaskDomains = new Schema ({
    label:{
        type: String,
        required: true
    }

});

如何编辑以下 post 方法来填充引用的 TaskDomain 架构。 这是我的方法:

import * as mongoose from 'mongoose';
import {taskSchema,TaskDomains} from '../models/tasks.model';
import {Request, Response} from 'express';

const Task = mongoose.model('Task', taskSchema);
const domain = mongoose.model('domain', TaskDomains);
export class taskController{
public addNewTask (req: Request, res:Response){
        let newTask = new Task();
        newTask.user = req.body.user;
        newTask.project = req.body.project;
        newTask.issue = req.body.issue;
        newTask.title = req.body.title;
        newTask.dateCreated = req.body.dateCreated;
        newTask.records = new domain(req.body._domain);
        newTask.records = new domain(req.body._domain.label);
        newTask.records = req.body.time;

        newTask.save((err, task)=>{
            if(err){
                res.send(err);
            }
            res.json(task);
        });
    }
    }
我需要帮助编辑 post 方法。我一直在尝试不同的方法,但都没有奏效。

【问题讨论】:

    标签: javascript mongodb typescript express schema


    【解决方案1】:

    你目前的方法有点不对,你需要先保存域文档,保存成功后可以创建任务文档。

    试试这个:

    public addNewTask (req: Request, res:Response){
    
        // create the domain document first, before creating the task document, and then store its _id in the task document
        let _domain = new domain(req.body._domain);
        _domain.save((err,_domain)=>{
            let newTask = new Task();
            newTask.user = req.body.user;
            newTask.project = req.body.project;
            newTask.issue = req.body.issue;
            newTask.title = req.body.title;
            newTask.dateCreated = req.body.dateCreated;
    
            // here you only need to store the _id of the newly created _domain document
            newTask.records = [{
                _domain : _domain._id,
                time : req.body.time
            }]
    
            newTask.save((err, task)=>{
                if(err){
                    res.send(err);
                }
                //if you want populated _domain object in your records array, you can use .populate()
                Task.populate(task,{path : records._domain},(err,task) =>                                 
                {
                   res.json(task);
                })
            });
        })
    }
    

    我假设,您的请求正文如下所示:

    {
        user : "user_name",
        project : "project_name",
        issue : "issue_name",
        title : "title_",
        dateCreated : "date",
        _domain : {
            label : "some_label"
        },
        time : 12345
    }
    

    【讨论】:

    • 非常感谢,我现在就试试看它是否有效。我如何在邮递员上发布记录?
    • 我想试试这个方法是否适用于邮递员:我如何填写正文:{“user”:“salma”,“project”:“9”,“issue”: "24", "title": "task3" "records":[ { "label":"test" },{"time":40}] }
    • 试试这样:{ "user": "salma", "project": "9", "issue": "24", "title": "task3", "_domain" : { "label":"test" },"time":40 }。如果您按照之前评论中的建议发送请求,则必须相应更改我提供的代码
    • 我得到以下信息: { "_id": "5c9c5456d5ef022b6db2f88e", "records": [ { "_id": "5c9c5456d5ef022b6db2f88f", "time": 40 } ], "user": "salma ", "project": "9", "issue": "24", "title": "task3", "__v": 0 } 标签未发布
    • 标签也贴出来了,但由于它是不同的文档(引用文档),它保存在域文档中,请参阅您在架构中使用了type : Schema.Types.ObjectId。如果您还想在保存后填充域,则需要使用.populate()。请阅读参考文档以及如何在 mongoose 中使用它们
    猜你喜欢
    • 2014-09-22
    • 2021-06-28
    • 2012-11-11
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2013-01-11
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多