Ken*_*lin 2 mongoose mongodb node.js
我正在使用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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以上内容来自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 } });
}
});
Run Code Online (Sandbox Code Playgroud)
我没有收到任何错误消息,Mongo的原始输出是:
{ updatedExisting: true,
n: 1,
connectionId: 78912,
err: null,
ok: 1 }
Run Code Online (Sandbox Code Playgroud)
但是当我查看我的集合的内容时,子文档仍然存在.我尝试过使用原生驱动程序,但没有运气.我的架构中有错误吗?提前感谢您花时间看看这个.
/肯利
好吧,Victor在mongoose谷歌小组中指出了这一点,所以对他赞不绝口.事实证明,对象没有被mongoose自动强制转换为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 } });
}
});
Run Code Online (Sandbox Code Playgroud)
如果有人想知道我将其添加到我的数据库原型中,那么我可以在任何地方访问ObjectId类型.
Database.prototype = {
objectId : mongoose.Types.ObjectId,
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助其他遇到类似问题的人.
| 归档时间: |
|
| 查看次数: |
1720 次 |
| 最近记录: |