是否可以通过仅在Mongoose中更新父对象来保存子对象

时间:2019-08-13 15:03:04

标签: node.js mongoose

我在Nodejs应用程序中使用猫鼬来保存两个架构:

需要对象:

let needSchema = new Schema({
dateCreated : { type: Date, default: Date.now },
detail : String,
quantity : Number,
delivered : Number
});

OrphanageHome对象

let orphanageHomeSchema = new Schema({    
    dateCreated : { type: Date, default: Date.now },
    detail : String,
    name: String,
    address: String,
    mobile:Number,
    email: String,
    noOfChildren: Number,
    needs : [{type: Schema.Types.ObjectId, ref: 'need'}],
    location: {
        type: {
            type: String,
            enum: ['Point'],
            required: true
        },
        coordinates: {
            type: [Number],
            required: true
        }
    }
});

每当我尝试使用新的Need对象更新现有的orphangehome对象时,它都无法保存Need对象并将其绑定到orphangehome对象。为什么?

    newNeed = {
    _id : "random",
    detail : 'random',
    quantity : 'random',
    delivered : 'random'

    } 

 existingOrphanageHome.needs.push[newNeed];

orphanageODM.findByIdAndUpdate(existingOrphanageHome._id, existingOrphanageHome, function (err, 
updatedOrphanageHome) {

        callback(err, updatedOrphanageHome);

    });

0 个答案:

没有答案