简体   繁体   中英

Node.js Mongoose update on embedded document with pull not persisting

I'm using mongoose 3.4.0 and MongoDB 2.0.6.

I have the following schema :

var Database = module.exports = function Database(){};

Database.prototype = {

  _model : {},
  _schema: {
     Comment : new Schema ({
  _id : ObjectId,
  comment : { type : String },
  date : { type : Date, default : Date.now },
  userId : { type : Schema.ObjectId, ref : 'User' },
  nickname : { type : String },
  profileLinkAn : { type : String },
  profileLinkIos : { type : String }
}),

Game : new Schema ({
  roomName : { type : String },
  openTime : { type : Date },
  closeTime : { type : Date, index : true },
  minPlayers : { type : Number },
  maxPlayers : { type : Number},
  numberOfPlayers : { type : Number, default : 0 },
  winner : { userId : { type : Schema.ObjectId, ref : 'User'} },
  runrUp : { userId : { type : Schema.ObjectId, ref : 'User' } },
  semiFn : [ { type : Schema.ObjectId, ref : 'User'} ],
  qtrFn : [ { type : Schema.ObjectId, ref : 'User' } ],
  rnd16 : [ { type : Schema.ObjectId, ref : 'User' } ],
  rnd32 : [ { type : Schema.ObjectId, ref : 'User' } ],
  prize : [ this.Prize ],
  tag : [ { type : String, index : true } ],
  status : { type : Number, index : true },
  businessType : { type : Number },
  mallId : { type : Schema.ObjectId, ref : 'Mall', index : true },
  registeredPlayers : [ { type : ObjectId, ref : 'User' } ],
  thumbnailImage : [ this.PrizeDetailImage ],
  gamePrice : { type : Number },
  slotPrice : { type : Number },
  comment : [ this.Comment ],
  commentCnt : { type : Number, default : 0 },
  wantUid : [ { type : Schema.ObjectId, ref : 'User' } ],
  wantCnt : { type : Number, default : 0 }
    })
 },

 connect : function(url) {

    mongoose.connect(url);
    this._model.Comment = mongoose.model('Comment',this._schema.Comment);

    this._model.Game = mongoose.model('Game', this._schema.Game);
},

model : function(model) {
    switch (model) {
        case 'Comment':
            return this._model.Comment;
        case 'Game':
            return this._model.Game;
    }
}

}

The above is from Database.js. Below is the code from my express app. I have left out some of the code for brevity. The problem I have seems to be with the query.

var Game = this.db.model('Game');
Game.update({ _id : req.body._id }, { $pull : { comment : { _id : req.body.commentId } }               }, function (err,numAffected,raw) {
if(err)
{
      res.json({ data : { success : false } });
}
else
{
    console.log(raw);
    res.json({ data : { success : true } });
}
});

I get no error message and the raw output from Mongo is :

{ updatedExisting: true,
  n: 1,
  connectionId: 78912,
  err: null,
  ok: 1 }

but when I look at the contents of my collection the sub-document is still there. I have tried using the native driver but had no luck. Have I done something wrong in my schema? Thanks in advance for taking time to look at this.

/Kenley

Ok I was pointed to this by Victor on the mongoose google group so kudos to him. It turns out that the ObjectId was not being cast to an ObjectId automatically by mongoose. This is how the query looks now :

Game.update({ _id : req.body._id }, { $pull : { comment :  { _id : this.db.objectId(req.body.commentId) } } }, function (err,numAffected,raw) {
                if(err)
                {
                    console.log(err);
                    res.json({ data : { success : false } });
                }
                else
                {
                    console.log(raw);
                    res.json({ data : { success : true } });
                }
            });

If anyone is wondering I added this to my Database prototype so I can access the ObjectId type anywhere.

Database.prototype = {

objectId : mongoose.Types.ObjectId,
}

Hope this can help anyone else who is having a similar problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM