我使用nodejs和mongodb。將對象推送到已存在的JavaScript對象
我從下面的MongoDB的查詢字典RES:
Profile.find(search,['_id', 'username'],function(err, res)
印刷資源的樣子:
[
{
"username": "dan",
"_id": "508179a3753246cd0100000e"
},
{
"username": "mike",
"_id": "508317353d1b33aa0e000010"
}
]
}
我想推到每個資源的[X]另一個鍵值對:
[
{
"username": "dan",
"_id": "508179a3753246cd0100000e",
"more info": {
"weight": "80",
"height": "175"
}
},
{
"username": "mike",
"_id": "508317353d1b33aa0e000010"
},
"more info": {
"weight": "80",
"height": "175"
}
]
}
我已經試過:
var x=0
dic = []
while (x<res.length){
dic[x] = {}
dic[x]=res[x]
dic[x]["more info"] = {"wight" : weight, "height" : hight}
x=x+1
}
但會被忽略,我得到
[
{
"username": "dan",
"_id": "508179a3753246cd0100000e"
},
{
"username": "mike",
"_id": "508317353d1b33aa0e000010"
}
]
}
感謝你的幫助。
(http://stackoverflow.com/editing-help) –
拼不出來的身高和體重? :) x在哪裏被定義?它在哪裏遞增? – epascarello