Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js Mongoose update on embedded document with pull not persisting
    primarykey
    data
    text
    <p>I'm using mongoose 3.4.0 and MongoDB 2.0.6.</p> <p>I have the following schema :</p> <p>var Database = module.exports = function Database(){};</p> <pre><code>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; } } } </code></pre> <p>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.</p> <pre><code>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 } }); } }); </code></pre> <p>I get no error message and the raw output from Mongo is :</p> <pre><code>{ updatedExisting: true, n: 1, connectionId: 78912, err: null, ok: 1 } </code></pre> <p>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.</p> <p>/Kenley</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload