我正在构建一个记事本,并且每个音符条目都内置了一系列标记。对于每个标记,我们然后创建一个新的Mongo条目,以#34; denormalize"伯爵。所以:
if (tag === null) {
// do nothing
} else {
// Check if tag exists
// If no, create it
// If yes, increase the count
if (Tags.findOne({owner: Meteor.userId(), name: tag}) === undefined) {
var newTag = {
owner: Meteor.userId(),
name: tag,
count: 1
};
Tags.insert(newTag);
} else {
Tags.update({owner: Meteor.userId(), name: tag}, {$inc: {count: 1}});
};
}
在我尝试删除标签之前,这很有效:
var deletedEntry = Entries.findOne(entryID);
Tags.update({owner: Meteor.userId(), name: deletedEntry.tags[0]}, {$inc: {count: -1}});
Tags.update({owner: Meteor.userId(), name: deletedEntry.tags[1]}, {$inc: {count: -1}});
Tags.update({owner: Meteor.userId(), name: deletedEntry.tags[2]}, {$inc: {count: -1}});
Tags.update({owner: Meteor.userId(), name: deletedEntry.tags[3]}, {$inc: {count: -1}});
Entries.remove(entryID);
它基本上会跳过减少某些标签的数量。它是随机的,令人难以置信。我使用Modulus + MongoHQ进行托管。如果你们能提供帮助,我将不胜感激。
答案 0 :(得分:0)
在Mongo中,如果更新语句要修改多个文档,则需要将选项multi set传递给true,默认情况下它只更新与选择器匹配的第一个文档(docs)。 例如:
Tags.update({
owner: Meteor.userId(),
name: { $in: deletedEntry.tags }
}, {
$inc: {count: -1}
}, { multi: true });