我有一系列与以下类似的文件:
{
_id: ObjectId("..."),
title: "...",
body: "...",
comments: [
{
name: "...",
email: "...",
comment: "..."
},
{
name: "...",
email: "...",
comment: "..."
},
{
name: "...",
email: "...",
comment: "..."
}
]
}
让我们说我想更新第二条评论的名称字段。所以,我的查询如下:
db.posts.update({_id: ""}, {$set: {"comments.2.name": "new name"}});
现在,我想知道怎样才能将数组元素(2)的索引作为node.js中的参数/变量传递?
答案 0 :(得分:1)
答案是简单地在代码中构造对象。对象键在JavaScript和大多数支持表示法的其他语言中进行stringify,因此只需在外部构建字符串并使用Object notaion方法:
<form action="javascript:goCalc('calcForm')" id="calcForm" name="calcForm">
<table align="center" border="0" cellpadding="3" cellspacing="1" width="100%">
<tbody>
<tr bgcolor="#bfd5ea">
<td style="font-size: 30px; color: f00; FONT-WEIGHT: bold; TEXT-ALIGN: center">
Donation:</td>
<td style="FONT-WEIGHT: bold; TEXT-ALIGN: center">
Tax Rate %:</td>
<td style="FONT-WEIGHT: bold; TEXT-ALIGN: center">
Net Cost of Donation</td>
<td style="FONT-WEIGHT: bold; color: 00f; TEXT-ALIGN: center">
Tax Savings</td>
</tr>
<tr bgcolor="#e6eef7">
<td align="middle" nowrap="nowrap" valign="center">
$
<input class="formtext" name="inputIncome" size="12">x</td>
<td align="middle" bgcolor="#e6eef7" nowrap="nowrap" valign="center">
<select class="formtext" id="inputPercentage" name="inputPercentage">
<option selected="selected" value="10">10%</option>
<option value="15">15%</option>
<option value="25">25%</option>
<option value="28">28%</option>
<option value="33">33%</option>
<option value="35">35%</option>
<option value="39.6">39.6%</option>
</select>=</td>
<td align="middle" valign="center">
$ <span style="FONT-WEIGHT: bold; TEXT-ALIGN: center"> <input class="formtext" id="inputCost" name="inputCost" readonly="readonly" size="12"> </span>
</td>
<td align="middle" valign="center">
$
<input class="formtext" id="inputMoney" name="inputMoney" readonly="readonly" size="12">
</td>
</tr>
<tr align="right">
<td nowrap="nowrap" valign="center">
</td>
<td nowrap="nowrap" valign="center">
<div align="center">
<input class="btn" name="Submit" type="submit" value="Calculate">
</div>
</td>
<td nowrap="nowrap" valign="center">
<div align="center">
</div>
</td>
<td nowrap="nowrap" valign="center">
</td>
</tr>
</tbody>
</table>
</form>
使用正确的“点表示法”格式构建对象