使用猫鼬模式插入数据mongodb /使用子文档制作猫鼬方案

时间:2019-03-12 15:34:57

标签: node.js mongodb mongoose schema

我对整个环境还很陌生,已经搜索了一段时间。 下面是我的Mongoose模式模型,我试图在Workflows集合中使用此模式进行工作流插入,我最初将模式中的每个子对象分解开,然后将它们全部组合为一个“ WorkflowInsert”。 idk,如果格式化正确或者我应该使用哪种方法插入对象。我觉得我在模型文件和插入之间缺少任何内容。

workflow.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.ObjectId;

const DstComponents = new Schema({
    ID: String,
    updated: { type: Date, default: Date.now }
});

const Transport = new Schema({
    ID: String,
    Type: String,
    Method: Boolean,
    Location: String,
    Name: String,
    Desc: String,
    SrcComponent: String,
    DstComponents: [DstComponents]
});


const IPort = new Schema({
    Type: String,
    Name: String,
    TransportID: String,
});

const OPort = new Schema({
    ID: String,
    Type: String,
    Name: String,
    Transport: [Transport]
});

const InputPorts = new Schema({
    Ports: [IPort]
});

const OutputPorts = new Schema({
    Ports: [OPort]
});

const Properties = new Schema({
    Ports: [{
        Name: String,
        Desc: String,
        Value: String,
    }]
});

const Component = new Schema({
    id: { type: String, required: true, unique: true },
    Type: String,
    Name: String,
    Desc: String,
    WorkflowID: ObjectId,
    Updated: { type: Date, default: Date.now },
    Properties: [Properties],
    InputPorts: [InputPorts],
    OutputPorts: [InputPorts]
});

const Workflow = new Schema({
    workflowid: ObjectId,
    id: String,
    LocalCachePath: String,
    SharedCachePath: String,
    Name: String,
    Desc: String,
    Components: [Component]
});

const WorkflowInsert = new Schema({
    workflowid: ObjectId,
    id: String,
    LocalCachePath: String,
    SharedCachePath: String,
    Name: String,
    Desc: String,
    Components: [{
        id: { type: String, required: true, unique: true },
        Type: String,
        Name: String,
        Desc: String,
        WorkflowID: ObjectId,
        Updated: { type: Date, default: Date.now },
        Properties: [{
            Name: String,
            Desc: String,
            Value: String,
        }],
        InputPorts: [{
            Ports: [{
                Type: String,
                Name: String,
                TransportID: String,
            }]
        }],
        OutputPorts: [{
            Ports: [{
                ID: String,
                Type: String,
                Name: String,
                Transport: [{
                    ID: String,
                    Type: String,
                    Method: Boolean,
                    Location: String,
                    Name: String,
                    Desc: String,
                    SrcComponent: String,
                    DstComponents: [{
                        ID: String,
                        updated: { type: Date, default: Date.now }
                    }]
                }]

            }]
        }]
    }]
});

module.exports = mongoose.model('Workflow', WorkflowInsert)
module.exports = {
    Workflow: Workflow,
    Component: Component,
    Properties: Properties,
    InputPorts: InputPorts,
    OutputPorts: OutputPorts,
    Transport: Transport,
    IPort: IPort,
    OPort: OPort,
    DstComponents: DstComponents
};

下面的代码是我的插入内容,我尝试了多种插入数据库的方法,但没有任何效果,我继续得到“未定义数据库”,我很确定它告诉我我的数据还是我的架构已损坏,无法插入。 有没有办法获得更好的错误报告。

api.js

router.get('/insertworkflow', (req, res) => {

    //const Profile = require('../models/Profile')
    const Workflow = require('../models/Workflow.js')
    const fs = require('fs');
    const path = require('path');
    const directoryPath = path.join(__dirname, '/');

    fs.readFile(directoryPath + '/workflowexampledata.json', 'utf8', function(err, data) {
        if (err) throw err;
        console.log(data);
        var json = JSON.parse(data);
        var data = { texto: json };
        db.collection("workflows").insert(data, function(err, res) {
            if (err) throw err;
        });
    });
});

定义数据库

///Connecting to MOngoDb
MongoClient.connect(url, { useNewUrlParser: true }, function(err, database){
    if (err) throw err;
    var db = database;
    console.log('connected to db mai');
});

0 个答案:

没有答案