[英]Can't $push a new array to document in Meteor Collection
我已经学习Meteor大约3个星期了,但仍在努力围绕更新/查询集合。 我正在尝试构建一个Slack克隆,并使用一组夹具文档创建了以下集合:
Conversations.insert({
channel: "#defaultChannel",
createdBy: "coffeemeup",
timestamp: new Date(),
followers: ["username1", "username2"],
entries: [
{
message: "this is a message #1",
postedTime: new Date(),
author: "coffeemeup"
}]
});
我正在尝试使用下面的代码将另一个文档插入entry数组。 但是,这不仅不起作用,而且还会引发“更改对象的[[Prototype]]”将导致您的代码运行非常缓慢的错误。 我真的很感谢您的帮助!
Conversations.update({
channel: "#defaultChannel"
}, {
$push: {
entries: {
message: newMessage,
postedTime: new Date(),
author: "coffeemeup"
}
}
});
另外,我很想听听有关如何更好地构建/设计该数据库以构建Slack克隆的建议。
如果要在客户端上运行更新操作,则需要使用_id
字段。 否则,您将收到此错误:
错误:不允许。 不受信任的代码只能按ID更新文档。 [403]
结果,首先获取文档,然后使用文档的_id
运行更新查询。
例如:
var conversation = Conversations.findOne({
"channel": "#defaultChannel"
});
Conversations.update({
_id: conversation._id
}, {
$push: {
entries: {
message: "newMessage",
postedTime: new Date(),
author: "coffeemeup"
}
}
});
这是更新后的对话文档的样子:
{
"_id": "wGixGJgoM6fk57mtN",
"channel": "#defaultChannel",
"createdBy": "coffeemeup",
"timestamp": "2015-07-27T19:25:52.842Z",
"followers": [
"username1",
"username2"
],
"entries": [
{
"message": "this is a message #1",
"postedTime": "2015-07-27T19:25:52.842Z",
"author": "coffeemeup"
},
{
"message": "newMessage",
"postedTime": "2015-07-27T19:27:54.930Z",
"author": "coffeemeup"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.