擅长:python、mysql、java
<p>您可以在更新中将<a href="http://docs.mongodb.org/manual/reference/operator/update/positional/#up._S_" rel="nofollow">the $ operator</a>与$elemMatch一起使用。我们先插入文档:</p>
<pre><code>collection.insert({
"_id": "23343",
"class": "Physics",
"students": [
{"id": "2412", "name": "Alice", "mentor": 0},
{"id": "2413", "name": "Bob", "mentor": 0}]})
</code></pre>
<p>现在我将运行两个更新语句,首先添加导师“Mark”,然后添加“Jackson”:</p>
^{pr2}$
<p>每个update语句只影响“students”数组中一个子文档的“mentor”字段。在</p>