Meteor/MongoDB引用其他文档中的特定子文档
我正在制作Meteor中的笔记应用程序原型;功能要求包括:Meteor/MongoDB引用其他文档中的特定子文档,mongodb,meteor,Mongodb,Meteor,我正在制作Meteor中的笔记应用程序原型;功能要求包括: 用户可以访问共享笔记 注释包含不同的部分 每个用户都需要能够在注释/章节中添加注释 标记可以随时间而保留(例如,添加到现有标记,而不更新或删除以前创建的标记) 符号在用户之间应该是私有的 如上所述,每个文档都有一个数据键,其中包含子文档数组-注释的每个部分。大概是这样的: { "_id" : ObjectId("someObjectID"), "owner" : "Q5mpJZnAtFN5EMWT9", "cr
数据
键,其中包含子文档数组-注释的每个部分。大概是这样的:
{
"_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..."
}
]
}
对于主notes文档,数据
数组将节存储为子文档;对于用户符号,data
数组将其个人符号存储为子文档。我的想法是使用parent
键来区分共享注释和用户注释:
parent:null
表示“顶层”,共享注释父项:“yG8xrh6KiZXv7e8MD”
的内容,用于指向用户符号的“顶级”注释或子文档。(希望这是有道理的)李>
两个问题。首先也是最重要的——这是一个有效的设计吗
如果它是有效的设计,那么如何引用特定的子文档?例如,在上面的文档中,如果用户只想在第2节中添加符号?我是否可以将
\u id
添加到子文档中,然后将该值用于注释文档中的父项
键 这不是完整的解决方案,只是一个示例:
我会这样做。我会稍微修改一下您的文档,在每个部分添加注释字段:
{
"_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;
}
});
因此,如果我理解上面的方法(Meteor/React的新方法,MongoDB的新方法…),它将只返回
note
对象,其中标记。userId
是登录用户-即使过滤器没有在该特定级别应用?(例如,tonote.data
而不是tonote.data.notations
?@drannos我编辑了一点。它应该返回注释,其中删除了所有不属于userId的注释。您可以像Meteor.call一样从模板端调用它('notes.singleNote','323',Meteor.userId(),函数(错误,结果){……});