Meteor/MongoDB 从另一个文档中引用特定的子文档



我正在 Meteor 中制作一个笔记应用程序的原型;功能要求包括:

  1. 用户有权访问共享备忘录
  2. 注释包含不同的部分
  3. 每个用户都需要能够向注释/节添加注释
  4. 符号
  5. 可以随着时间的推移而保留(例如,添加到现有符号而不更新或删除以前创建的符号)
  6. 符号在用户之间应该是私有的

鉴于上述情况,每个文档都有一个包含子文档数组的data键 - 注释的每个部分。像这样:

{ 
"_id" : ObjectId("someObjectID"),
"owner" : "Q5mpJZnAtFN5EMWT9",
"createdAt" : "2018-01-05T22:56:03.257Z",
"updatedAt" : "2018-01-06T12:07:03.123Z",
"parent" : null,
"title" : "Note Title",
"data" : [
{
"date" : "2018-01-05T22:56:03.257Z",
"title" : "Section 1 Title", 
"text" : "Section content goes here..."
},
{
"date" : "2018-01-05T22:56:03.257Z",
"title" : "Section 2 Title", 
"text" : "Section content goes here..."
}
]
}

对于主注释文档,data数组将节存储为子文档;对于用户表示法,data数组将其个人注释存储为子文档。我的想法是使用parent键来区分共享笔记和用户符号:

  1. "顶级"、共享笔记parent : null
  2. 类似于parent : "yG8xrh6KiZXv7e8MD"指向用户表示法的"顶级"注释或子文档。(希望这是有道理的)。

两个问题。首先 - 这是一个有效的设计吗?

如果它是有效的设计,那么我该如何引用特定的子文档?例如,在上面的文档中,如果用户只想在第 2 节中添加符号?是否可以向子文档添加_id,然后将该值用于表示法文档中的parent键?

这不是完整的解决方案,而只是一个示例:

我会做这样的事情。我会稍微修改一下您的文档,在每个部分添加符号字段:

{ 
"_id" : ObjectId("someObjectID"),
"owner" : "Q5mpJZnAtFN5EMWT9",
"createdAt" : "2018-01-05T22:56:03.257Z",
"updatedAt" : "2018-01-06T12:07:03.123Z",
"parent" : null,
"title" : "Note Title",
"data" : [
{
"date" : "2018-01-05T22:56:03.257Z",
"title" : "Section 1 Title", 
"text" : "Section content goes here...",
"notations": [
{
_id: "some id",
version:1
userId: "fsajksffhj",
date: "2018-01-05T22:56:06",
note: "some note about this sectioon"
},
{
_id: "some id2",
version:1,
userId: "fsajksffhj",
date: "2018-01-05T22:56:06",
note: "some note about this sectioon"
},
{
_id: "some id1",
version:1,
userId: "fsajksffhj",
date: "2018-02-06T00:56:06",
note: "edited the first notation"
}
]
},
{
"date" : "2018-01-05T22:56:03.257Z",
"title" : "Section 2 Title", 
"text" : "Section content goes here..."
}
]
}

符号在用户之间应该是私有的

这是更难的部分。我会使用流星方法来做到这一点。另一种方法是使用MongoDB的聚合功能进行匹配,展开,重新匹配,分组和再次创建文档。如果使用其中任何一个,则正在使用反应性。

Meteor.methods({
'notes.singleNote: function(noteId, notationsUserId) {
check(noteId, String);
check(notationsUserId);
let note = Notes.findOne(noteId);
// remove other users' notations
note.data = note.data.map(function(data) {
if (data.notations) {
data.notations = data.notations.filter(function(d) {
return d.userId === notationsUserId;
});
}
return data
});
});

return note;
}
});

最新更新