某些文档具有以下架构:
{
doc: {
matrix: [
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
[{..}, {..}, {..}, {..}, {..}, {..}, {..},],
]
}
}
我是否可以仅使用一个查询从index = 2
中删除包含doc.matrix
的列?
我有一个使用N
查询的任务的解决方案(其中N
= 行中的行的数量)。但这种解决方案不是最佳的..
也许您可以建议另一个模式来解决此任务。
将忽略像“使用SQL数据库”这样的建议:)
有什么想法吗?
答案 0 :(得分:1)
所以我想你所指的是第二个"列"而不是" row"。考虑以下形式,这是一个更好的说明:
{
"matrix": [
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
]
}
所以一个查询表单确实是:
db.matrix.update(
{},
{
"$pull": {
"matrix.0": 2,
"matrix.1": 2,
"matrix.2": 2,
"matrix.3": 2,
"matrix.4": 2,
"matrix.5": 2,
"matrix.6": 2,
"matrix.7": 2,
"matrix.8": 2,
"matrix.9": 2,
}
}
)
这可能就像你在" N"中提到的那样。但这只是在一个方面做到了:
{
"matrix" : [
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 0, 1, 3, 4, 5, 6, 7, 8, 9 ]
]
}
当然,你的整个"指数"介词定义您可以通过某种方式识别实际位于子文档中的元素,但基本前提保持不变。
因此,通常由您在子文档中或在更广泛的意义上在这些行中具有这样的标识符:
{
"matrix": [
[
{ "index": 0, "ident": "A" },
{ "index": 1, "ident": "B" },
{ "index": 2, "ident": "C" }
],
[
{ "index": 0, "ident": "A" },
{ "index": 1, "ident": "B" },
{ "index": 2, "ident": "C" }
]
]
}
声明:
db.matrix.update(
{},
{
"$pull": {
"matrix.0": { "index": 1 },
"matrix.1": { "index": 1 },
}
}
)
结果如下:
{
"matrix" : [
[
{ "index" : 0, "ident" : "A" },
{ "index" : 2, "ident" : "C" }
],
[
{ "index" : 0, "ident" : "A" },
{ "index" : 2, "ident" : "C" }
]
]
}