【问题标题】:Node.js Mongoose update on embedded document with pull not persistingNode.js Mongoose 更新嵌入式文档,拉取不持久
【发布时间】:2012-11-20 20:18:15
【问题描述】:

我正在使用 mongoose 3.4.0 和 MongoDB 2.0.6。

我有以下架构:

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;
    }
}

}

以上来自 Database.js。下面是我的快递应用程序的代码。为简洁起见,我省略了一些代码。我的问题似乎与查询有关。

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 } });
}
});

我没有收到任何错误消息,Mongo 的原始输出是:

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

但是当我查看我收藏的内容时,子文档仍然存在。我曾尝试使用本机驱动程序,但没有运气。我在架构中做错了吗?提前感谢您花时间查看此内容。

/肯利

【问题讨论】:

  • 您能发布更多代码吗?语法不完整,查看您在 Mongoose 中实际注册模型的行会很有用。
  • 您确定req.body.commentId 与您正在更新的Game 文档中评论的_id 匹配吗?
  • 是的,JohnnyHK 我已经确保游戏文档中的req.body.commentId 和_id 是一样的。

标签: node.js mongodb mongoose


【解决方案1】:

好的,Victor 在 mongoose google 小组中向我指出了这一点,因此向他表示敬意。事实证明,猫鼬没有将 ObjectId 自动转换为 ObjectId。这就是查询现在的样子:

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 } });
                }
            });

如果有人想知道我将此添加到我的数据库原型中,以便我可以在任何地方访问 ObjectId 类型。

Database.prototype = {

objectId : mongoose.Types.ObjectId,
}

希望这可以帮助遇到类似问题的其他人。

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 2011-12-21
    • 2011-12-04
    • 2014-06-12
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多