2017-02-16 165 views
0

我是新的mongodb和貓鼬。我想知道如果我可以創建一個包含兩個子文檔的數組,並且只在子文檔中插入(如果我想),以及如何訪問子文檔?貓鼬陣列文件

var User = mongoose.Schema({ 

    username: String, 
    password: String, 
    UserToken: String, 

    //here is what I want to do 
    messagesArray: { 
    FromUserid: String, 
    messege: String 
    } 

}); 

回答

2

你只需要包住messagesArray與[]

var User = mongoose.Schema({ 

    username: String, 
    password: String, 
    UserToken: String, 

    messagesArray: [{ 
    FromUserid: String, 
    messege: String 
    }] 
}); 

見貓鼬接受的所有數據類型,http://mongoosejs.com/docs/schematypes.html