将mongodb文档存储在另一个文档中

将mongodb文档存储在另一个文档中,mongodb,mongoose,Mongodb,Mongoose,我正在尝试将MongoDB文档从一个集合保存到另一个集合。我希望在通过API端点进行检索时它看起来像这样。我单独创建错误,并希望通过id将其与机器连接 { "_id": "59634780c464263b28a6891a", "name": "GLUE-SM-21", "status": false, "__v": 0, "error": { "_id" : ObjectId("59769b9ad1050f244cadfced"),

我正在尝试将MongoDB文档从一个集合保存到另一个集合。我希望在通过API端点进行检索时它看起来像这样。我单独创建错误,并希望通过id将其与机器连接

{
    "_id": "59634780c464263b28a6891a",
    "name": "GLUE-SM-21",
    "status": false,
    "__v": 0,
    "error": {
        "_id" : ObjectId("59769b9ad1050f244cadfced"),
        "count" : 5,
        "name" : "Error-001-J",
        "__v" : 0
    }
}
但我明白了

{
    "_id": "59634780c464263b28a6891a",
    "name": "GLUE-SM-21",
    "status": false,
    "__v": 0,
    "error": [
        "59769b9ad1050f244cadfced"
    ]
}
这里我附上我目前的工作

错误模式

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var ErrorSchema   = new Schema({
    name: String,
    count: Number
});

module.exports = mongoose.model('Error', ErrorSchema);
var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;

var Error = require('./error');

var MachineSchema   = new Schema({
    name: String,
    status: Boolean,
    error: [{ type: ObjectId, ref: 'Error', default: null }]
});

module.exports = mongoose.model('Machine', MachineSchema);
机器架构

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var ErrorSchema   = new Schema({
    name: String,
    count: Number
});

module.exports = mongoose.model('Error', ErrorSchema);
var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;

var Error = require('./error');

var MachineSchema   = new Schema({
    name: String,
    status: Boolean,
    error: [{ type: ObjectId, ref: 'Error', default: null }]
});

module.exports = mongoose.model('Machine', MachineSchema);
在默认情况下,没有错误。这是我的保存代码

        var machine = new Machine();        // create a new instance of the Machine model
        machine.name = req.body.name;  // set the machine name (comes from the request)
        machine.status = 1; // set the machine status (comes from the request)
        machine.error = null;

        machine.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Machine created!' });
        });
这是我的更新代码

Machine.findById(req.params.machine_id, function(err, machine) {

            if (err)
                res.send(err);

            machine.name = machine.name;

            if(req.body.error && machine.status) {
                machine.status = false;
            } else if(!req.body.error && !machine.status) {
                machine.status = true;
            }
            machine.error = req.body.error;
            machine.save(function(err) {
                if (err)
                    res.send(err);

                io.emit('machine', machine);

                res.json({ message: 'Machine updated!' });
            });

        });

我找到了解决办法

我已将错误文档的对象ID作为错误属性保存在我的机器文档中。我已经使用mongo上的填充函数更新了我的代码

Machine.find().populate("error").exec(
            function(err, machines) {
            if (err)
                res.send(err);

            res.json(machines);
        }
        );

问题在于
MachineSchema
“still”只需要存储
ObjectId
数组。因此,即使您尝试以不同的格式存储,“模式”也会出错或“强制转换”到已注册的类型。您需要用新模式注册一个新模型,或者在不使用任何模式的更“原始”的代码中进行转换。然后,您当然需要注册一个适合新数据结构的模式。