2015-11-19 60 views
0

這是我的架構:如何訪問貓鼬中的對象屬性?

var userSchema = new mongoose.Schema({ 
    username: String, 
    total: { 
    avg_time: Number, 
    avg_guesses: Number, 
    guessed: Number 
    } 
}); 

我想創建一個收集和avg_time,設置屬性avg_guesses和avg_guessed 0 我想這個代碼,但它不工作。

var user_data = new userSchema({username: usernames[i], total.avg_time: 0, total: avg_guesses: 0, total: guessed: 0}); 

回答

0

您的代碼創建一個名爲USER_DATA新的模式,而不是建立在MongoDB中的文檔。 您必須將模式編譯爲模型,然後創建文檔。例如,

var user = mongoose.model('user', userSchema); 

var user_data = new user({ 
         username:'foo', 
         total:{ 
          avg_time: 0, 
          avg_guesses: 0, 
          guessed: 0 
         } 
       }); 

我建議查看貓鼬的文檔。點擊here