2015-12-07 102 views
0

更新MongoDB的阵列,当我尝试更新剂量不要扔回去了它会OK的任何错误,但是当我检查我的DATEBASE没有我自己的更新没有被修改,请帮助如何使用猫鼬

this is my db 
{ 
    "_id" : ObjectId("56651f0e4905bd041cad0413"), 
    "creator" : ObjectId("566299dd17990464160ae27a"), 
    "content" : "this is my joke 2", 
    "created" : ISODate("2015-12-07T05:54:22.858Z"), 
    "__v" : 15, 
    "comments" : [ 
     { 
      "posteruserId" : "5665e6867185d87c1e71dbdc", 
      "postedBy" : "lawrence nwoko", 
      "postterscomment" : "good joke", 
      "_id" : ObjectId("56660745f644c2501116acce") 
     }, 
     { 
      "posteruserId" : "5665e6867185d87c1e71dbdc", 
      "postedBy" : "lawrence nwoko", 
      "postterscomment" : "good joke", 
      "_id" : ObjectId("56660b6d33c245c012104fdc") 
     } 
    ] 
} 

这是我的架构

var mongoose = require('mongoose'); 

var Schema = mongoose.Schema; 

var JokesSchema = new Schema({ 
    creator: {type: Schema.Types.ObjectId, ref: 'User'}, 
    content: String, 
    created:{type:Date, default: Date.now}, 
    comments: [{ 
     text: String, 
     postedBy: String, 
     posteruserId :String, 
     date: String, 
     postterscomment:String 
    }] 
}) 
module.exports = mongoose.model('Jokes_db', JokesSchema) 

这里我我的帖子funtion api.post( '/更新',函数(REQ,RES){

//  Joke.findById("56651f0e4905bd041cad0413", function (err, meeting) { 
      Joke.update({_id: "5665e6867185d87c1e71dbdc", 'comments._id' : "56660745f644c2501116acce"}, 
       {'$set': { 
        'comments.$.postterscomment': "working" 
       }}, 

       function(err, numAffected) { 
        if(err){ 
         console.log(err) 
        }else{ 
         res.json(numAffected) 
        } 
       } 
      ); 
     }); 
+0

你确定你的模式在这里是正确的吗?如果没有,则发布定义的模式。主要在这里指出'.update()'中用于主文档的'_id'与这里显示的文档不匹配。所以如果这是你期望更新的文档,那么'_id'值是错误的。因此5665e6867185d87c1e71dbdc似乎是'comments.postuserId'值,而不是''56651f0e4905bd041cad0413“',实际上它是'_id'字段。 –

+0

我刚刚添加了我的猫鼬Schema请看看它 –

+0

但是你是否注意到所提及的错误的'_id'值?这似乎是这里的问题。 –

回答

0

已经三天试图解决这个问题,但他的恩典我没有帮助问题的用户,我没有使用正确的编号作出查询感谢您的帮助球员我希望这可以帮助其他用户

api.post('/editecomments', function(req, res) { 
     Joke.update({_id: "56651f0e4905bd041cad0413", 'comments._id' : "56660745f644c2501116acce"}, 
      {'$set': {'comments.$.postterscomment': 'working'}}, 

      function(err, numAffected) { 
       if(err){ 
        console.log(err) 
       }else{ 
        res.json(numAffected) 
       } 
      } 
     ); 
    });