Javascript Can';t$推送一个新阵列到Meteor Collection文档中
我已经学习Meteor大约3周了,现在仍在努力更新/查询收藏。我正在尝试构建Slack克隆,并使用一组fixture文档创建了以下集合:Javascript Can';t$推送一个新阵列到Meteor Collection文档中,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我已经学习Meteor大约3周了,现在仍在努力更新/查询收藏。我正在尝试构建Slack克隆,并使用一组fixture文档创建了以下集合: Conversations.insert({ channel: "#defaultChannel", createdBy: "coffeemeup", timestamp: new Date(), followers: ["username1", "username2"], entries: [ {
Conversations.insert({
channel: "#defaultChannel",
createdBy: "coffeemeup",
timestamp: new Date(),
followers: ["username1", "username2"],
entries: [
{
message: "this is a message #1",
postedTime: new Date(),
author: "coffeemeup"
}]
});
我正在尝试使用下面的代码将另一个文档插入到条目数组中。但这不仅不起作用,还会引发“改变对象的[[Prototype]]会导致代码运行非常缓慢…”错误。我真的很感谢你的帮助
Conversations.update({
channel: "#defaultChannel"
}, {
$push: {
entries: {
message: newMessage,
postedTime: new Date(),
author: "coffeemeup"
}
}
});
此外,我还想听听关于如何更好地构造/设计此数据库以构建松弛克隆的建议。如果您想在客户端上运行更新操作,则需要使用
\u id
字段。否则将出现以下错误:
错误:不允许。不受信任的代码只能按ID更新文档。
[403]
因此,首先获取文档,然后使用文档的\u 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"
}
]
}
您应该使用
$set
而不是$push
我想他确实想使用$push
也许可以试试前面定义为字符串的var newMessage?我认为这个错误是在抱怨正在插入的值与集合中已经存在的值的类型不同。例如,如果第一条“消息”是字符串,第二条消息是整数