2015-12-07 99 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) 
       } 
      } 
     ); 
    });