Documents.update(
{_id: Session.get("current_document_id")},
{$push: {schema: {type: "text", size: size, name: name, label: label}}}
);
上面的查询是Meteor集合,'Documents.update'映射到MongoDB文档中的'db.documents.update'(http://docs.mongodb.org/manual/applications/update/) . 通过该查询,我可以在主文档中添加模式文档 . 子文档存储在一个数组中:
Document:
schema:
array:
{type: "text", size: 6, name: "first_name", label: "First name"},
{type: "text", size: 6, name: "last_name", label: "Last name"}
我想使用此查询修改子文档的名称和大小属性:
Documents.update(
{_id: Session.get("current_document_id"), 'schema' : "first_name"},
{$push: {schema: {type: "text", size: 7, name: name, label: "First Name2"}}}
);
但是该操作直接在模式下附加一个新对象并删除该数组:
Document:
schema:
{type: "text", size: 7, name: "first_name", label: "First Name2"}
如何修改查询以更改属性以避免此问题?查询后我想有这个文件:
Document:
schema:
array:
{type: "text", size: 7, name: "first_name", label: "First name2"},
{type: "text", size: 6, name: "last_name", label: "Last name"}
1 回答
您可以使用
$set
操作更新现有数组元素,该操作使用$位置运算符来标识选择器中匹配的数组元素,如下所示:这将使匹配的
schema
元素替换为$set
对象中包含的元素 .如果您只想更新目标
schema
元素的各个字段,则可以使用点表示法 . 例如,要仅更新size
和name
字段: