mongoose 中 update 用 save 修改失效,是什么原因

 我来答
匿名用户
2016-09-22
展开全部
mongoose 中 update 用 save 修改失效,是什么原因
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394//数据库是:{ "__v": 1, "_id": "538f5f0f6195a184108c8bd8", "title": "GameTitle", "item": [{ "_id": "538f5f0f6195a184108c8bd6", "name": "itemOne", "men": [{ "_id": "5390cccf0a84f41f37082874", "user": "id22222222", "score": 2000 }, { "_id": "2390cccf0a84f41f37082873", "user": "id33333333", "score": 1000 }] }, { "_id": "538f5f0f6195a184108c8bd7", "name": "itemTwo", "men": [] }], "status": 1} //代码是: var MenSchema = new mongoose.Schema({ user: 'String', score: { type: Number, default: 0 } }); var ItemsSchema = new mongoose.Schema({ name: String ,men: [MenSchema] }); ListsSchema = new mongoose.Schema({ title: { type: String, required: true } ,item: [ItemsSchema] }); var Items = mongoose.model('item', ItemsSchema); var Lists = mongoose.model('lists', ListsSchema); var Men = mongoose.model('men', MenSchema);Insert and update: function commit(sId, sItem, sUser, sIncreaseScore) { Lists.findOne({, "_id": sId, "item.name": sItem }, null, function(err, documents) { if (!err) { if (documents != null) { Lists.findOne({ "_id": sId, "item.name": sItem, "item.men.user": sUser }, null, function(err, subDoc) { if (!err) { if (subDoc != null) { //increase user score //!!!!!!!!!!!!!But subDoc will get all arrays of item.men, so I can't update it correctly } else { //inser new user score var userData = new Men({ user: sUser, score: sScore }); documents.item[0].men.push(userData); documents.save(function(err) { if (!err) { ///!!!!!!!!!!!!!!Will come this console.log("documents error on save!"); } else { console.log("save documents ok!"); } }); } } }); } } else { console.log("not find the game item!"); } } );}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式