使用mongoose

时间:2017-02-10 04:35:39

标签: node.js mongodb mongoose mongoose-schema

这是我的预订架构

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

let booking = {
    statusNumber: { type: Number },
    service: {
        hair: {
            avaliable: { type: Number },
            accepted: { type: Number },
            acceptedBy: [{
                artistId: { type: String },
                noOfServices: { type: Number }
            }]
        },
        makeUp: {
            avaliable: { type: Number },
            accepted: { type: Number },
            acceptedBy: [{
                artistId: { type: String },
                noOfServices: { type: Number }
            }]
        },
    }
}
module.exports = mongoose.model('booking', booking)

我正在尝试使用以下代码更新我的预订

const acceptBooking = {
    method: "POST",
    path: "/artist/acceptBooking",
    handler: (req, res) => {
        console.log(req.payload)
        services.booking.get({ _id: req.payload.bookingId }, (err, result) => {
            if (err || result.lenght == 0) {
                res("error")
            } else {
                let hairAcceptedBy = {
                    artistId: req.payload.techId,
                    noOfServices: req.payload.hair
                }
                let makeUpAcceptedBy = {
                    artistId: req.payload.techId,
                    noOfServices: req.payload.makeUp
                }
                console.log(hairAcceptedBy)
                console.log(makeUpAcceptedBy)
                let updateObject = {
                    service: {
                        hair: {
                            avaliable: result[0].service.hair.avaliable - req.payload.hair,
                            accepted: (result[0].service.hair.accepted == undefined ? 0 : result[0].service.hair.accepted) + req.payload.hair,
                            $push:{acceptedBy: hairAcceptedBy}
                        },
                        makeUp: {
                            avaliable: result[0].service.makeUp.avaliable - req.payload.makeUp,
                            accepted: (result[0].service.makeUp.accepted == undefined ? 0 : result[0].service.makeUp.accepted) + req.payload.makeUp,
                            $push:{acceptedBy: makeUpAcceptedBy}
                        }
                    }
                }

                services.booking.update({ _id: req.payload.bookingId }, updateObject, (err, affected, result) => {

                    console.log("AFFECTED>>>>>", affected)
                    console.log("RESULT>>>>>", result);
                    res("booking done")
                })

            }
        })

我的更新功能看起来像

exports.update = (data, updateData, callback) => {
    console.log('mongo log for update function', data)
    modelUser.update(data, updateData, callback);

}

我的更新功能正在运行并且更新可用并且已接受但是 没有推入acceptedBy数组

1 个答案:

答案 0 :(得分:0)

这个方法对我有用,你可以用这个

modelUser.findByIdAndUpdate(
        info._id,
        {$push: {"messages": {title: title, msg: msg}}},
        {safe: true, upsert: true, new : true},
        function(err, model) {
            console.log(err);
        }
    );