2014-02-19 26 views
1

我有以下更新查詢:

var where = 'answers.round_' + ans.round + '_' + ans.iteration 

Game.findByIdAndUpdate(gameId, { 
    $addToSet: { 
     where: ans 
    } 
}, function(err, model) { 
    if (err) 
     throw err 
    console.log('after game update with answer ' + JSON.stringify(model)) 
    callback() 
}) 

和數據庫結構如下:

"_id: ObjectId("5304bc1dcf36941e3adcd3fd"), 
"answers" : { 
    "round_1_1" : [], 
    ... 
    } 

問題是俺們對象未被推入"round_1_1"陣列(不是我檢查它不是null)。回調中的模型是我想更新的模型,只是更新沒有發生。

我的問題是類似this的東西,它像點符號不工作(即使在控制檯上輸出answers.round_1_1

回答

1

你修整把它推到不存在的where陣列。因爲有是在你的模型沒有這樣的領域,新的Monggose發送到MongoDB的

問題是你是如何構建您的查詢嘗試下面的代碼:。!

var query = {$addToSet: {}} 
    , where = 'answers.round_' + ans.round + '_' + ans.iteration; 

query.$addToSet[where] = ans; 

Game.findByIdAndUpdate(gameId, query, function(err, model) { 
    // Your callback function 
}) 
+0

哇,我真的需要仰望實況更詳細的考慮! – Pio