2012-10-02 38 views
0

考慮以下簡單模式:使用Mongoose,如何通過表單將鍵值對作爲對象插入到數組中?

var PostSchema = new Post({ 
    body : String, 
    tags : [] 
} 

通過表單POST(即我開發REST HTTP API),我想插入的鍵/值對進入tags陣列使得所得mongodb的文件將看起來像:

{ 
    "body" : "This is the post body", 
    "tags" : [ 
       {"Color" : "Orange"}, 
       {"Color" : "Blue"}, 
       {"Person" : "Ted"}, 
       {"Person" : "Fred"}, 
       {"Person" : "Joe"}, 
       {"Stone" : "Diamond"} 
      ] 
} 

我的問題是,我該如何命名爲tags表單域做到這一點?我在想每種顏色都像tags[0][color],但這不起作用,它爲tags數組中的每個鍵值和一個列表創建了一個單獨的數組。

回答

0

現在我有點傻了。解決的辦法是(顯然)提供tags像這樣:

tags[0][color] = orange 
tags[1][color] = blue 
tags[2][person] = ted 
tags[3][person] = fred 
tags[4][person] = joe 
tags[5][stone] = diamond 

我的大腦被困在他們的所有分組到tags[0] - 不知道爲什麼。

相關問題