1. 程式人生 > >Saving a model fails with mongo error: MongoError: Unknown modifier: $pushAll

Saving a model fails with mongo error: MongoError: Unknown modifier: $pushAll

Order.findById(orderId)
        .populate(
            [{
                path: 'prescription'
            }]
        )
        // .lean()
        .then(order => {
            theOrder = order;
            let checkLogInfo = {
                orderId: order._id,
                prescription: order.prescription.toJSON(),
                checkResult: order.checkResult
            }
            let checkLog = new CheckLog(checkLogInfo);
            return checkLog.save();
        })
        .then(newCheckLog => {
            theNewCheckLog = newCheckLog;
            return Prescription.update({_id: theOrder.prescription._id}, {prescriptionInformation: req.body.prescriptionInformation});
        })
        .then(() => {
            console.info(theOrder.checkLog);
            theOrder.checkLog.push(theNewCheckLog._id);
            console.info(theOrder.checkLog);
            theOrder.checkResult = null;
            return theOrder.save();
        })
        .then(() => {
            return res.send({code: '400000', messageInfo: ['The update was successful.']});
        })
        .catch(err => next(err));
  • 由於 theOrder.checkLog.push(theNewCheckLog._id); 這句,在mongoose儲存時報錯 MongoError: Unknown modifier: $pushAll
  • 原因:版本衝突
  • 解決辦法:在新建schema的時候加上{usePushEach: true}配置就可以了
let OrderSchema = new Schema({
    doctor: {
        type: Schema.ObjectId,
        ref: 'user'
    },
    pharmacist: {
        type: Schema.ObjectId,
        ref: 'user'
    }
}, {timestamps: {createdAt: 'created', updatedAt: 'updated'}, usePushEach: true});