2014-07-16 23 views
0

我在使用Mongoose在MongoDB中創建新文檔時遇到問題。必須在MongoDB中新建一個與Schema完全相同的文檔嗎?

新文檔必須與模式完全相同嗎?

我的意思是,我有以下模式:

ar userSchema = new Schema({ 

     userID: Number, 
     userName: String, 
     userEmail: String, 
     teams:Array, 
     fbUID: String, 
     googleUID: String, 
     twitter: String 


}); 

但要創造一個新的用戶文檔我沒有所有的領域,所以我的文件將不包含所有字段作爲上述架構。

例如,我正在根據上面的Schema添加一個新文檔。我的文檔是:

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

var user = new users({ 
     userID: id, //give the id of the next user in Dbase 
     userName: userName, 
     userEmail: 'userEmail', 
     teams:[], 
     fbUID: '1234' 
    }); 

user.save(function(err, user){ 
     if(err) return console.error(err); 
     log.d("user salved", user); 
    }); 

所以,我沒有所有的字段,並且我目前無法保存任何文檔。

如果我在新文檔中沒有Schema中的所有字段,這有什麼關係嗎?

UPDATE:

和第二個問題是,我得到的其中之一:

fbUID, 
     googleUID, 
     twitter 

通過一個功能,我不知道那些我接受了,所以我想保存文檔是這樣的:

function salveUser(userName, socialMediaType, socialMediaID){ 

    var id; 



     users.count(function(err, count){ 
     if(err){ 
      log.d("Err in counting files") 
     } 
     id = count; 

     }); 

    var user = new users({ 
     userID: id, //give the id of the next user in Dbase 
     userName: userName, 
     userEmail: 'userEmail', 
     teams:[], 
     socialMediaType : socialMediaID 
    }); 


    user.save(function(err, user){ 
     if(err) return console.error(err); 
     log.d("user salved", user); 
    }); 

    currentSession = sessionOBJ.login(id, socialMediaID); 

    return currentSession; 
} 

}

這就是爲什麼我想知道Mongoose是通過我在函數中接收的單詞切換文檔中的密鑰,還是使用了我正在放置的確切單詞,在本例中爲「socialMediaType」。

有人知道嗎?

+0

你的代碼看起來很好。你能否包括一個確切的異常? –

+0

是的。我會更新這個問題。 – debeka

回答

1

回答你的主要問題:不,新文檔不應與模式完全相同。

事實上,除非您明確地將其中的一些標記爲required,否則其模式中定義的任何文檔字段都不需要以保存它。

但是您在salveUser函數中犯了幾個錯誤。


首先,你id = count分配將需要很長時間後user文件將被創建和保存的地方,因爲users.count是異步的。這意味着您的示例中的userID將始終爲udefined

要解決它,你應該等待users.count嘗試使用它的結果之前完成:

users.count(function(err, count){ 
    if(err){ 
    return log.d("Err in counting files") 
    } 

    var user = new users({ 
    userID: count, //give the id of the next user in Dbase 
    userName: userName 
    /* other fields */ 
    }); 

    user.save(/* your callback */); 
}); 

其次,它看起來像你想的值socialMediaID分配給字段與名稱等於socialMediaType的值。但是你不能像你這樣做。因此,您不是設置fbUID/googleUID字段,而是嘗試設置未出現的socialMediaType字段。

要解決它,你應該使用[]分配:

var user = new users({ 
    userID: id, //give the id of the next user in Dbase 
    userName: userName, 
    userEmail: 'userEmail' 
}); 
user[socialMediaType] = socialMediaID; 

既然你沒有對userID領域的唯一索引,因爲MongoDB自動分配一個唯一的_id的所有文件,你應該能夠順利.save()你的文件。


但是在你的問題中你說你無法做到這一點,這意味着你的代碼中有其他問題。請發佈一個確切的例外,你從mongoose中獲得.save()的回調。

+0

謝謝你的回答。我真的很感激。 :) – debeka

1

它看起來像你的代碼是罰款,列昂尼德在評論

我會盡量保存用戶的所有直接值,以排除不設置任何變量....

所以說試試這個:

var user = new users({ 
    userID: 11, //give the id of the next user in Dbase 
    userName: 'John', 
    userEmail: '[email protected]', 
    teams:['Blue', 'Green'], 
    fbUID: '1234' 
}); 

如果這樣的作品,那麼問題可能是該iduserName變量沒有被擊中其實這個消息之前設定。

您不必僅僅因爲它們在模型中就爲所有字段賦值。

祝你好運!

相關問題