【发布时间】:2021-12-27 16:45:08
【问题描述】:
我想创建一个查询,将每个 entityType 的元素数限制为最大 N=2。
除了entityType & entityId,原始文档还有一些其他属性(例如:timestamp),为了简单起见,我只是将其删除。
这是初始/参考文档。
{
"_id" : ObjectId("100000"),
"agency" : "agency_1",
"username" : "user_one",
"recentEntities" : {
"entities" : [
{
"entityType" : "type_one",
"entityId" : "11",
"other" : "aa",
},
{
"entityType" : "type_one",
"entityId" : "12",
"other" : "ab",
},
{
"entityType" : "type_two",
"entityId" : "21",
"other" : "ba",
}
]
}
}
这里是这个问题的 3 个规范/案例:
- 每次添加一个新实体作为
entities数组中的第一个 元素时,表示最近可见的实体。
假设我想用以下实体更新初始文档:
{
"entityType" : "type_two",
"entityId" : "22",
"other" : "bb",
}
由于我们没有达到"entityType" = "type_two" 的限制,我们只需将对象添加到数组中,更新后的文档将如下所示:
{
"_id" : ObjectId("100000"),
"agency" : "agency_1",
"username" : "user_one",
"recentEntities" : {
"entities" : [
{
"entityType" : "type_two",
"entityId" : "22",
"other" : "bb",
},
{
"entityType" : "type_one",
"entityId" : "11",
"other" : "aa",
},
{
"entityType" : "type_one",
"entityId" : "12",
"other" : "ab",
},
{
"entityType" : "type_two",
"entityId" : "21",
"other" : "ba",
}
]
}
}
- 如果带有特定
entityId的文档已经存在,但对象内的其他字段已更改,那么我想将该实体对象替换为最近的。
使用此实体更新 reference 文档:
{
"entityType" : "type_one",
"entityId" : "12",
"other" : "xy",
}
将导致:
{
"_id" : ObjectId("100000"),
"agency" : "agency_1",
"username" : "user_one",
"recentEntities" : {
"entities" : [
{
"entityType" : "type_one",
"entityId" : "12",
"other" : "xy",
},
{
"entityType" : "type_one",
"entityId" : "11",
"other" : "aa",
},
{
"entityType" : "type_two",
"entityId" : "21",
"other" : "ba",
}
]
}
}
- 另一方面,如果已达到限制,则将删除特定类型的最旧实体。
例如通过添加以下实体:
{
"entityType" : "type_one",
"entityId" : "13",
"other" : "ac",
}
我们需要删除 "entityId" = "12" 并将新的放在上面。
更新后,参考文档将如下所示:
{
"_id" : ObjectId("100000"),
"agency" : "agency_1",
"username" : "user_one",
"recentEntities" : {
"entities" : [
{
"entityType" : "type_one",
"entityId" : "13",
"other" : "ac",
},
{
"entityType" : "type_one",
"entityId" : "11",
"other" : "aa",
},
{
"entityType" : "type_two",
"entityId" : "21",
"other" : "ba",
}
]
}
}
我设法做到了前 2 点,但最后一点实施起来有点棘手,因此非常感谢任何帮助。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework